Well its just typical that after researching for ages and eventually
resigning myself to mailing the list I find an answer. Using a callback post
validator. So for anyone else that has a similar issue here is the way to
get it done:

First, set the post validator as such:

$this->validatorSchema->setPostValidator(
            new sfValidatorCallback(array('callback'=>array($this,
'checkDates'))));

What this does is tell the form to pass validation of the content of the
form values to a function called checkDates. Then create the new function
inside your forms class as such:

public function checkDates($validator, $values)
  {
    if (empty($values['end_date']))
    {
      return $values;
    }
    else if (strtotime($values['start_date']) >
strtotime($values['end_date']))
    {
      throw new sfValidatorError($validator, 'NEW VALIDATOR!! The start date
needs to be before the end date');
    }

    return $values;

  }

You can obviously do whatever you need to validate the contents of your own
validator but as you can see here I first check to see if end_date is empty.
If so, alls good and continue (remember that start_date is validated to have
a date in it before the post validator is called so I don't need to do it
again). If, however, my end_date is not empty and start_date is greater than
end_date, throw exception error for the form class to handle. Otherwise,
just return.

A good example I found of the callback post validator can be found here:
http://www.symfony-project.org/cookbook/1_2/en/conditional-validator

On Tue, Nov 23, 2010 at 9:54 AM, Alexandre Salomé <
[email protected]> wrote:

> You have to use sfValidatorOr, with a custom validator "isNull".
>
> Take a look at source code of validators, very interesting.
>
> Alexandre
>
> ---
> Alexandre Salomé - http://alexandre-salome.fr
>
> Le 23 nov. 2010 08:47, "Gareth McCumskey" <[email protected]> a écrit :
>
>
> Hi all,
>
> I have a unique requirement in one of my forms where I need to do date
> validation. My problem is that I need a start date to be entered
> ("required"=>true), end date is optional (as it can run indefinitely, but
> the I also need to validate that the start date is less than the end date. I
> am having trouble setting up the validators for this.
>
> Currently I have the following to meet the required start but optional end
> date:
>
> "start_date"=>new sfValidatorDate(array(
>         "min"=>date('Y-m-d'),
>       ), array(
>         "min"=>"Date used cannot be earlier than today",
>         "required"=>"A start date is required",
>         "invalid"=>"This needs to be a date in the format YYYY-MM-DD"
>       )),
>       "end_date"=>new sfValidatorDate(array("required"=>false), array(
>         "invalid"=>"This needs to be a date in the format YYYY-MM-DD"
>       ))
>
> And I have also created a post validator to ensure start < end:
>
> $this->validatorSchema->setPostValidator(
>   new sfValidatorSchemaCompare('start_date',
> sfValidatorSchemaCompare::LESS_THAN_EQUAL, 'end_date',
>                     array('throw_global_error' => true),
>                     array(
>                       "invalid"=>"The start date needs to be before the end
> date"
>                     )))
>
> The problem with this, if end_date is left blank the post validator throws
> the error.
>
> --
> Gareth McCumskey
> http://garethmccumskey.blogspot.com
> twitter: @garethmcc
>
> --
> If you want to report a vulnerability issue on symfony, please send it to
> security at symfony-project.com
>
> You received this message because you are subscribed to the Google
> Groups "symfony users" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]<symfony-users%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/symfony-users?hl=en
>
>  --
> If you want to report a vulnerability issue on symfony, please send it to
> security at symfony-project.com
>
> You received this message because you are subscribed to the Google
> Groups "symfony users" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]<symfony-users%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/symfony-users?hl=en
>



-- 
Gareth McCumskey
http://garethmccumskey.blogspot.com
twitter: @garethmcc

-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en

Reply via email to