views:

20

answers:

1

I have a zend form with a start- and an endate.

$form = new Zend_Form();
$form->setName($this->formName)
     ->setMethod('post');

$feStartdate = new Zend_Form_Element_Text('startdate');
$feEnddate = new Zend_Form_Element_Text('enddate');

$form->addElement($feStartDate)
     ->addElement($feEndDate)
     ->addElement('submit', 'submit', array('label' => 'Save'));

I assume I must write a custom validator for to check this. How would this custom validator look like and how can I call it? I assume something like

$feEnddate->addValidator('dateComesAfter', $feStartDate)
A: 

Yep, custom validator, combined with Zend_Date::isLater() would be the way to go.

delsurf
I assume the validator should be attached to the enddate. In the function isValid($value) in the validator the $value is the value of the enddate. How do I get the value of the startdate in the validator?
murze
Sorry for the delay - didn't get the email when you responded.yes - you can add it to the enddate, and in the isValid($value, $context) function, you want to use $context['field_name'] to access the field you want to compare to. To abstract the validator - you would set the field name to compare to in the constructor.Constructor would be :.. construct($fieldName, $fieldTitle) ..and in isValid($value, $context = null) - you have:$otherFieldValue = $context[$this->_otherFieldName];Obviously, this is a rudimentary example, but I hope it illustrates how you can do it.
delsurf