PHP Magento Tips

How to validate Start date and End date in Custom Module in Magento

Rate this posting:
{[['']]}

Today i will show you one important thing of Magento validation in input form.

One of the most difficult remarkable validation for every developer when creating a input type of date field with start date and End date.

To validate start date and end date is very easy in magento, because magento uses famous java-script validation.js.

you can easily validate Start date and End date in magento custom module.

you will get validation message as per below screenshot.




Just add below code.
$fieldset->addField(

            'start_date',

            'date',

            array(

                'label' => Mage::helper('company_extension')->__('From Date'),

                'name'  => 'start_date',

                'note'    => $this->__('Select your start date.'),

                'required'  => true,

                'class' => 'required-entry validate-date validate-date-range date-range-attribute-from',



            'image' => $this->getSkinUrl('images/grid-cal.gif'),

            'format'  => Mage::app()->getLocale()
                                ->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),

           )

        );



        $fieldset->addField(

            'end_date',

            'date',

            array(

                'label' => Mage::helper('company_extension')->__('End Date'),

                'name'  => 'end_date',

                'note'    => $this->__('Select your end date.'),

                'required'  => true,

                'class' => 'required-entry validate-date validate-date-range date-range-attribute-to',



            'image' => $this->getSkinUrl('images/grid-cal.gif'),

            'format'  =>Mage::app()->getLocale()
                         ->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),

           )

        );

* just add validate-date,  validate-date-range and date-range-attribute-from and
date-range-attribute-to class.

--> replace attribute with your Entity code name.
Tag : Magento
0 Comments On "How to validate Start date and End date in Custom Module in Magento"

Back To Top