Question

I want to set maximum value of "Date of Birth" (dob) attribute of the customer.

I want to set this date only earlier than today (not for future). After change I want to have JS validation on adminhtml backend in customer edit screen.

I want to update this attribute in setup scripts within my module. How to achieve this?

Thanks in advance for your help.

Was it helpful?

Solution

You need to add a new JavaScript validation to the Validation object and tell the "Date of Birth" attribute to use it.

  1. Add a layout update file for the backend area and tell Magento to load a JavaScript file

    In your config.xml add a section like this:

    <config>
        <!-- ... -->
        <adminhtml>
            <layout>
                <updates>
                    <emzee_dobvalidation>
                        <file>emzee_dobvalidation/emzee_dobvalidation.xml</file>
                    </emzee_dobvalidation>
                </updates> 
            </layout>
        </adminhtml>
    </config>
    

    Create the file app/design/adminhtml/default/default/layout/emzee_dobvalidation/emzee_dobvalidation.xml and add this content:

    <?xml version="1.0"?>
    <layout>
        <default>
            <reference name="head">
                <action method="addJs"><script>emzee_dobvalidation/validation.js</script></action>
            </reference>
        </default>
    </layout>    
    
  2. Add the JavaScript validation to the Validation object

    Create the file js/emzee_dobvalidation/emzee_dobvalidation.js containing this code:

    Validation.add('validate-customer-dob','Date of birth cannot be in the future!',function(v){
        var test = new Date(v);
        var now = new Date();
    
        return test.getTime() <= now.getTime();
    });
    

    You will not be allowed to set a date in the future.

  3. Reconfigure the "Date of Birth" attribute to use the validation

    Make sure your extension setup class uses a class inheriting Mage_Eav_Model_Entity_Setup, e.g. Mage_Customer_Model_Entity_Setup.

    Add this to your config.xml:

    <config>
        <global>
            <resources>
                <emzee_dobvalidation_setup>
                    <setup>
                        <module>Emzee_DobValidation</module>
                        <class>Mage_Customer_Model_Entity_Setup</class>
                    </setup>
                </emzee_dobvalidation_setup>
            </resources>
        </global>
    </config>
    

    Create the setup script updating your attribute (app/code/community/Emzee/DobValidation/sql/emzee_dobvalidation_setup/install-0.0.1.php):

    <?php
    
    $installer = $this;
    
    $installer->startSetup();
    
    $entityTypeId = $installer->getEntityTypeId('customer');
    $installer->updateAttribute($entityTypeId, 'dob', 'frontend_class', 'validate-customer-dob');
    
    $installer->endSetup();
    
  4. Make sure everything worked

    If everyting went ok you will get an error when submitting a date in the future:

    No unborn customers anymore!

Please note that as requested this only is a JavaScript validation. You may want to also check for the correct input on the server side. As this wasn't requested I left it as a home exercise.

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