14

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

2 Answers 2

8

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.
12
  • Two questions: 1) How does this work -> 'class' => 'required-entry validate-date validate-date-range date-range-custom_theme- to'` 2) How to input date-range-your_attribute_code-from` as I do not have my custom attribute code.
    – Jordan
    Commented Jul 24, 2014 at 5:02
  • @Jordan how are you making the form if you do not have your attribute code? Technically it does not matter as long as the two have the same thing in attribute_code it is just to link the two fields. Commented Jul 24, 2014 at 6:39
  • I just used class mentioned in app/code/core/Mage/Adminhtml/Block/Cms/Page/Edit/Tab/Design.php and it worked. Do I need to change it ?
    – Jordan
    Commented Jul 24, 2014 at 8:00
  • 1
    @Jordan you need the -from and -to suffixes on your classes. Try date-range-start_data-from and date-range-start_data-to Commented Jul 24, 2014 at 8:40
  • 1
    Thanks a lot for clear explanation. What matters is a unique attrib code for -to and -from to check date range.
    – Jordan
    Commented Jul 24, 2014 at 11:39
0

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

2
  • Do you have the customized code that will work ?
    – Jordan
    Commented Jul 22, 2014 at 4:13
  • I would highly discourage modifying the Magento core file js/prototype/validation.js. You can add custom validations in a separate JS file...
    – 7ochem
    Commented Mar 3, 2015 at 14:03

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.