Magento – Limit text field size in product custom attribute to 80 characters

adminhtmlce-1.7.0.2product-attribute

I have just created a custom attribute (standard text field) on a simple product. I'd like to limit this field to 80 characters as it's to be used as an eBay title to integrate with eBay.

I only want to limit this field though, not all text fields.

Can someone advise how I can do this?

Thanks

Best Answer

To my knowledge Magento does not allow the limiting of lengths for EAV attributes. For a varchar this gets stored in the table catalog_product_entity_varchar which has the definition value => varchar(255).

One option you could do is to add a backend model for this attribute. In this model you could write your own beforeSave function that will process the length of given value and trim it to be the desired length. The class should extend Mage_Eav_Model_Entity_Attribute_Backend_Abstract

public function beforeSave($object)
{
    $attrCode = $this->getAttribute()->getAttributeCode();
    if ($object->hasData($attrCode)) {
        $object->setData($attrCode, substr($object->getData($attrCode),0,50));
    }
    return $this;
}