Question

I have been working on admin form where I want the user to select Start Date and End Date.

I have to provide validation such that the End Date must be greater than Start Date.

enter image description here

Could anyone pls help me in providing defautl Magento validation class for start and end date with the scenario as explained above?

If its not possible with default Magento validation, pls post custom JS code.

Also this is in my form for date range

        $dateFormatIso = Mage::app()->getLocale()->getDateFormat(
            Mage_Core_Model_Locale::FORMAT_TYPE_SHORT
        );

$fieldset->addField('start_date', 'date', array(
            'label'     => Mage::helper('myhelper')->__('Start Date'),
            'name'      => 'start_date',
            'note'      => $this->__('Start Date'),
            'required'  => true,
            'image'     => $this->getSkinUrl('images/grid-cal.gif'),
            'format'    => $dateFormatIso,
             'class'    => 'required-entry validate-date validate-date-range date-range-start_date-from'
        ));

        $fieldset->addField('end_date', 'date', array(
            'label'     => Mage::helper('myhelper')->__('End Date'),
            'name'      => 'end_date',
            'note'      => $this->__('End Date'),
            'required'  => true,
            'image'     => $this->getSkinUrl('images/grid-cal.gif'),
            'format'    => $dateFormatIso,
            'class'     => 'required-entry validate-date validate-date-range date-range-end_date-to'
        ));

Thanks

Was it helpful?

Solution

Try using Magento's validator's validate-date-range class on your fields.

If you add this to both fields then it will get the to date and compare the two values.

If get the to date value using the following code:

var m = /\bdate-range-(\w+)-(\w+)\b/.exec(elm.className);
if (!m || m[2] == 'to' || Validation.get('IsEmpty').test(v)) {
    return true;
}

So what you will need to do is add a class in the format date-range-your_attribute_code-from and date-range-your_attribute_code-to. Doing this will mean that Magento is able to link the two fields.

For an example of this check out the design tab on CMS pages. app/code/core/Mage/Adminhtml/Block/Cms/Page/Edit/Tab/Design.php

The following is a more detailed explanation about the classes:

  1. validate-date: this makes sure that the entered value is a valid date,
  2. validate-date-range: this does the comparison between two dates when the have the correct classes,
  3. date-range-custom_data-from, date-range-custom_data-to: these link the two dates so that they can be used in the date range check, note that these should be the same apart from the -to and -from means it can be date-range-xxxx-from and date-range-xxxx-to respectively.

OTHER TIPS

All of the validation are tied to the Java Script validation rules defined under

js/prototype/validation.js

File

You can customize you code there

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