I have this form in Zend where the user should set up start & end date of something. I'm trying to invalidate the form / field if the end date is behind start date but it's not working. I overwritten the isValid() method and it worked but right now something else it's not working just because the isValid() method is overwritten.
how am i doing things:
$req = $this->getRequest();
$form->setAction($req->getRequestUri());
if($req->isPost() && $form->isValid($req->getPost())) {
$startTime = strtotime(date($form->getValue('live_start')));
$endTime = strtotime(date($form->getValue('live_end')));
if($startTime > $endTime){
$form->live_end->addError("End time shouldn't be earlier than start time !!!");
}
If I use die; inside the if() it will die. So it's guaranteed that that if it's working. I used all kind of methods but it's not working.
Anyone have some ideas why it doesn't?
I recommend you to :
Create a new validator, For example YouApp_Validate_Date_GreaterThan
Attach it to the live_end field.
Override your form isValid
method, in such a way
:
public function isValid($data)
{
$this->live_end->getValidator('GreaterThan')->setDate($data['live_start']);
return parent::isValid($data);
}
In your validator implementation, set the Date to compare to as a property to easily set it dynamically ->setDate($yourDate)
Hope it helps.