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
.
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
Best Answer
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:
So what you will need to do is add a class in the format
date-range-your_attribute_code-from
anddate-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:
validate-date
: this makes sure that the entered value is a valid date,validate-date-range
: this does the comparison between two dates when the have the correct classes,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 bedate-range-xxxx-from
anddate-range-xxxx-to
respectively.