create a custom validator that extends sfValidatorSchema

$this->mergePostValidator(myValidatorSchemaCustom);

all submitted values are passed to its doBind method.

this is an example

class myValidatorSchemaChangePassword extends sfValidatorSchema
{
  public function __construct($password, $new, $repeat, $options =
array(), $messages = array())
  {
    $this->addOption('password', $password);
    $this->addOption('new', $new);
    $this->addOption('repeat', $repeat);
    $this->addRequiredOption('user');

    $this->addMessage('mismatch', 'Passwords did not match');

    parent::__construct(null, $options, $messages);

    $this->setMessage('invalid', 'Wrong password.');
  }

  protected function doClean($values)
  {
    if (null === $values)
    {
      $values = array();
    }

    if (!is_array($values))
    {
      throw new InvalidArgumentException('You must pass an array
parameter to the clean() method');
    }

    if(empty($values[$this->getOption('password')]))
    {
      throw new sfValidatorErrorSchema($this, array($this->getOption
('password') => new sfValidatorError($this, 'required')));
    }

    if(!$this->getOption('user')->checkPassword($values[$this-
>getOption('password')]))
    {
      throw new sfValidatorErrorSchema($this, array($this->getOption
('password') => new sfValidatorError($this, 'required')));
    }

    /*more validation here*/

    return $values;
  }
}

On Jan 23, 3:53 am, Alexandru-Emil Lupu <[email protected]> wrote:
> maybe ... dunno
>
> $this_is_your_form_instance->setPostValidators(new yourCustomValidator());
>
>
>
>
>
> On Thu, Jan 21, 2010 at 1:25 PM, johnnygri <[email protected]> wrote:
> > Hi there,
>
> > I have a rather complicated form of 50+ fields. The value of one of
> > the first fields dictates whether validation should be performed on
> > approximately half of the fields or not. These fields all need to be
> > validated using native Symfony validators. As directed by the
> > documentation I suspected that this is a time for a post-validator.
>
> > However, it isn't clear to me how I go about using native Symfony
> > validators within a post-validator, infact I'm pretty sure it won't
> > work having run a simple test. I'm also wondering whether a custom
> > validator schema might be the answer, but I can't quite see how (I've
> > read the advanced forms page).
>
> > The solution I've arrived at is a custom bind() method that calls a
> > configureValidation() method, passing it the $taintedValues array.
> > This configureValidation() method then defines the validators
> > according to the submitted data and it works nicely.
>
> > My main question is, would you regard this solution as a bit hacky -
> > could there be a more elegant way of doing this?
>
> > Thanks
> > John
>
> > --
> > 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%2bunsubscr...@goog 
> > legroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/symfony-users?hl=en.
>
> --
> Have a nice day!
> Alecs
>
> As programmers create bigger & better idiot proof programs, so the universe
> creates bigger & better idiots!
> I am on web:  http://www.alecslupu.ro/
> I am on twitter:http://twitter.com/alecslupu
> I am on linkedIn:http://www.linkedin.com/in/alecslupu
> Tel: (+4)0722 621 280

-- 
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