0

I'd really like to limit the title length on products in Magento.

What I've tried is adding 'maxlength' => 65 somewhere in \app\code\core\Mage\Adminhtml\Block\, without success.

Does someone know how to add this feature? In HTML it will just be adding length="65" maxlength="65".

Thanks for all affords. :)

Maarten Klok
  • 215
  • 3
  • 20

3 Answers3

0

I don't have the platform available to give you a proper walkthrough on setting this up, but I should be able to get you in the right place. First of all, do not make changes in the app/code/core files. Any changes you would absolutely need to make to those files you should do by making a copy of lets say app/code/core/Mage/Sales/something.php to app/code/local/Mage/Sales/something.php. Magento knows to automatically use the code in local to override the code in core.

If you take a look at the source code for that page you'll see where the name form is:

<input id="name" name="product[name]" value="" class=" required-entry input-text required-entry" type="text"/>            </td>
<td class="scope-label"><span class="nobr">[STORE VIEW]</span></td>
    </tr>

What's going on here is you'll notice under class we have "required-entry input-text and, well, required-entry again. These are the validation tags defined in js/prototype/validation.js. You will need to add some custom validation to it, and add it to your template file (not in core, it can break when you upgrade).

You'll notice in validation.js a section

Validation.add('IsEmpty', '', function(v) {

In this section you can add your custom validation. Lets say:

//make sure these are unique, I'm not checking
['validate-length', 'Your input needs to be less than x characters.', function(v) {
    if (v.length > x) return false;
}],

If you need help finding the template location, take a look at: Finding Correct Templates and Blocks in Magento. You'll simply add validate-length class such as: class="required-entry validate-length..."

Community
  • 1
  • 1
Sturm
  • 4,105
  • 3
  • 24
  • 39
  • 1
    Templates for rendering attribute inputs are not locked to one attribute. Using the above advice, I think that OP can just add the classname to the `frontend_class ` column for the meta_title attribute in `eav_attribute` after creating a validate-length65 rule as you've suggested. – benmarks Jun 28 '12 at 11:40
  • Witch file in `\app\code\core\Mage\Adminhtml\Block\ ` do I have to edit to simply add `length="65" maxlength="65"` to the ``-field? Checking afterwards doesn't seem the best option. – Maarten Klok Jun 28 '12 at 14:15
0

After almost 10 hours of searching I gave the up the "best" way, and choose for the roundabout.

Simply add

document.getElementById("name").setAttribute("maxlength", "65");
document.getElementById("name").setAttribute("length", "65");

to app/design/adminhtml/default/default/template/catalog/wysiwyg/js.phtml

Maarten Klok
  • 215
  • 3
  • 20
0

You can add javascript validator to the product's name attribute. To achieve this, you need to update attribute with special value for frontend class. Just create sql upgrade:

$this->updateAttribute(
    Mage_Catalog_Model_Product::ENTITY,
    'name',
    array(
        'frontend_class' => 'validate-length maximum-length-65',
        'note'           => 'Max length is 65 characters'
    )
);
Roman Snitko
  • 3,655
  • 24
  • 29