Question

For a client we're exporting all the address information that goes into Magento into a third party processing system. The details about this is unimportant - but what is important is that things go terribly wrong if fields are longer than an expected amount of characters.

For this reason, we're using the API with substr in order to truncate the data down to the expected size - but this isn't really good enough. The staff using the admin panel is apparently of a decent size and can't be trained to use it correctly, so we also need to apply this limitations to the admin panel itself.

As we don't really care about them using technical know-how to get past these limitations, all we really need is a maxlength="x" on the form elements. It is a requirement that different form elements may have different maxlengths.

What is the cleanest, most upgrade-proof way to enforce a maximum length on the customer address fields in the admin panel?

My first thought was using JavaScript - but the fields don't have really predictable identifiers, so I'm not sure I can easily. Because they all use the form rendering stuff, I also can't just edit a template file by creating a custom admin theme.

Phrased another way: What's the easiest and cleanest way to add a maxlength attribute to these fields?

Was it helpful?

Solution

In method _afterToHtml add this:

    if($this instanceof Mage_Adminhtml_Block_Widget_Form_Renderer_Fieldset_Element)
    {
        //this code will prevent update unnecessary items in other parts of admin area
        if(!strpos($html, 'account[')) {
            return $html
        }

        if(strpos($html, 'account[prefix]')) {
            $length = 15;
        } elseif(strpos($html, 'account[firstname]')) {
            $length = 25;
        } elseif(strpos($html, 'account[lastname]')) {
            $length = 35;
        //.....
        }

        $string = '<input maxlength="' . $length . '" ';
        return str_replace('<input ', $string, $html);
    }

Inside maxlength param put the value you like.

upd.1

If you like you can include getStoreConfig instead of constant value

$length = Mage::getStoreConfig(......)

upd.2

Here are some more ideas.

Where to store values with max length?

  • directly in method
  • system / configuration ( Mage::getStoreConfig() )
  • as new field of customer attribute

Store values directly in method is the easiest way. But it looks not very good. You also can store value in system / configuration, but you need write additional config. And the 3rd variant is much better. But here there are a lot of disadvantages. Firstly magento CE doesn't have attribute grid and attribute edit pages. Secondly you need to customize existing attribute by adding new field. Theoretically you can take some extension like this http://www.magentocommerce.com/magento-connect/manage-customer-attributes.html and a little customize it (it doesn't have such field as maxlength - to get it's value in template). In my opinion, if you don't need to edit length values a lot of times - just use the first variant and you will save much time.

For example you decided how add this field. You need to decide where you need to replace the code. I think rewrite method _afterToHtml($html) is normal way, because magento created this method for such purposes.

And one more thing: magento EE supports all features you like. Sure it's to expensive to install EE. Hope this information can be helpful.

OTHER TIPS

If using Magento Enterprise, the length of these attributes can be set in Customers > Attributes > Manage Customer Address Attributes

You can add the javascript line:

setMaxLengths = function() {
    $$("input[name^='address']").each(function (obj) { obj.setAttribute('maxlength','256'); });
}; Event.observe(window, "load", function(){ setInterval(function(){ setMaxLengths(); }, 1000); });

(edited to add heartbeat so it works on dynamical loaded DOM elements...)

to the end of the /js/prototype/validation.js file and it will enforce the maxlimit you set on both the frontend and admin for all address text fields. Just replace 256 with the actual length limit you want.

also, make note of the file change so you can redo it after any upgrade. ideally you'd put it in its own js file that you include via xml, but the above is the quickest way to add it to all the correct locations. I'd personally roll it into a custom magento module, I'm ocd about doing everything the right magento way, but that is beyond the scope of the question. :-) Hope this helps.

If you want to add different maxlength to different fields, just add multiple java script lines with more specific selectors. this is certainly the easiest way!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top