4

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?

5
  • Why don't you use maxlength? w3schools.com/tags/att_input_maxlength.asp
    – zhartaunik
    Commented Apr 9, 2015 at 18:44
  • @zhartaunik I'm assuming you didn't read my question? The goal is to use maxlength, the question is how
    – Navarr
    Commented Apr 9, 2015 at 18:47
  • I read your post twice. Do you need the filename and place where you should add maxlength?
    – zhartaunik
    Commented Apr 9, 2015 at 19:39
  • @zhartaunik This is in the admin panel.. it's not like there's a template file I can just add maxlength to
    – Navarr
    Commented Apr 9, 2015 at 19:41
  • In that case you need anyway customization with adding method getStoreConfig, which can get property from system/configuration
    – zhartaunik
    Commented Apr 9, 2015 at 19:44

3 Answers 3

4
+50

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.

9
  • I sincerely doubt this is the cleanest way to do it.. right? It also doesn't help that I want to set different maxlengths for different fields (I've now added that to the question as I didn't specify it before)...
    – Navarr
    Commented Apr 13, 2015 at 13:27
  • Updated. If you want I can offer you the variant with renderer for each field. Second variant visually can be more pretty, but it's too complicated. You will need to create about 10 renderers and include them.
    – zhartaunik
    Commented Apr 13, 2015 at 13:48
  • @shartaunik Ah, I see. This makes a lot of sense (in that it's just a way of going about it I wouldn't think of). In this instance I would be extending the Mage_Adminthml_Block_Customer_Edit_Tabe_Addresses and adding the _afterToHtml method?
    – Navarr
    Commented Apr 13, 2015 at 13:59
  • I think the best usage will be inside event core_block_abstract_to_html_after or in rewrite Mage_Adminhtml_Block_Widget_Form_Renderer_Fieldset_Element::afterToHtml($html). If you will have some problems with it I can try to do this tomorrow morning.
    – zhartaunik
    Commented Apr 13, 2015 at 14:06
  • I have this working excellently by listening to core_block_abstract_to_html_after. It's also incredibly clean. My only concern is that it's a bit fragile, but I'm not sure there's a "better way." I'm going to mark this as the answer for now, but I'm going to hold off on the bounty in case someone else says something amazing - but unless that happens, the bounty is yours! Thanks for all of your help :)
    – Navarr
    Commented Apr 13, 2015 at 15:14
1

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

0

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!

5
  • This won't work. For example, Adding a new customer address means that these restrictions won't be added to it, since the dom elements did not exist on window load.
    – Navarr
    Commented Apr 17, 2015 at 14:36
  • @Navarr - Thanks for catching that, I modified the above code so that it works on dynamically loaded elements and tested. Verified that it is working 100% now.
    – ReDesigned
    Commented Apr 17, 2015 at 19:03
  • Yes.. that... will do that. It's not the cleanest way to do it, but is a solution
    – Navarr
    Commented Apr 17, 2015 at 19:06
  • The upside is that it works in all magento versions, frontend and backend, it won't conflict with other modules, it automatically works with third party onepage checkout modules... etc. I'd put it in its own js file that you include via xml, then it is 100% magento standards friendly, and cleaner then overriding the field render. Anyway, just another solution that is super quick and easy to implement. < 5min total dev time.
    – ReDesigned
    Commented Apr 17, 2015 at 19:21
  • This works for me in the backend but unfortunately nit on the front. I did flush the java cache. We are using mag 1.9.2.4.
    – Sparc
    Commented Aug 9, 2016 at 1:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.