Call the expert: How to implement a conditional validator?

Jon works on a symfony 1.1 project with a classic login form.
The form is composed of two fields: a username and a password. The validation rules are quite straightforward:
- He wants each field to be required
- He wants to check the correctness of the password
Here is the first implementation of the login form:
class loginForm extends sfForm { public function configure() { $this->setWidgets(array( 'username' => new sfWidgetFormInput(), 'password' => new sfWidgetFormInputPassword(), )); $this->setValidators(array( 'username' => new sfValidatorString(array('required' => true)), 'password' => new sfValidatorString(array('required' => true)), )); $this->widgetSchema->setNameFormat('login[%s]'); } }
This form enforces the requirements on the username and password fields but does not check the correctness of the password.
The password can only be validated if you also have access to the username value. But as you might know, a validator attached to a field does not have access to the other values of the form.
When a validator relies on another submitted value, you need to create a post validator. A post validator is executed after all other validators and is given the whole array of cleaned up values.
To check the password, we will implement a simple callback validator to ensure that the submitted password is equal to the username. Of course, for his real project, Jon will have to call the model layer to do the actual work.
Here is the modified login form with the added post validator:
class loginForm extends sfForm { public function configure() { $this->setWidgets(array( 'username' => new sfWidgetFormInput(), 'password' => new sfWidgetFormInputPassword(), )); $this->setValidators(array( 'username' => new sfValidatorString(array('required' => true)), 'password' => new sfValidatorString(array('required' => true)), )); $this->widgetSchema->setNameFormat('login[%s]'); // add a post validator $this->validatorSchema->setPostValidator( new sfValidatorCallback(array('callback' => array($this, 'checkPassword'))) ); } public function checkPassword($validator, $values) { if ($values['password'] != $values['username']) { // password is not correct, throw an error throw new sfValidatorError($validator, 'Invalid password'); } // password is correct, return the clean values return $values; } }
Now, the form works as expected, but Jon does not like one small thing: if you submit a random password without entering a username, you will have two error messages: a required error for the username and a global error for the wrong password.
But wait a minute, if the username is empty, we don't need to validate the password. Let's change the form to only validate the password if there is a username submitted. This is quite simple, as we just need to ensure that $values['username']
is not empty in the post validator callback:
public function checkPassword($validator, $values) { // before validating the password, check that the username is not empty if (!empty($values['username']) && $values['password'] != $values['username']) { throw new sfValidatorError($validator, 'Invalid password'); } return $values; }
As Jon is quite fussy, he would rather have the 'Invalid password' error message just above the password field instead of having a global error.
That's quite easy to accomplish by throwing an error bound to the password
field instead of throwing a global error:
public function checkPassword($validator, $values) { if (!empty($values['username']) && $values['password'] != $values['username']) { $error = new sfValidatorError($validator, 'Invalid password'); // throw an error bound to the password field throw new sfValidatorErrorSchema($validator, array('password' => $error)); } return $values; }
Jon is now happy with his login form and he is ready to take up the next challenge.
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.
After reading this and seeing the flexibility I'm going to find some time to make the move.
One thing, why are postValidator called if validators failed ?
Because !empty($values['username']) is redundant with first validation rule, or I missed some use case ?
I'm looking forward to read more from the expert !
P.S. I translated this article into Japanese in my blog.
I'll use this to finish off the My First Project for 1.1, hopefully this weekend, so we can publish it on the 1.1 docs page.
You ought to make this basic stuff more simpler.
Thanks anyway for this great framework !!
Just curious to know which one is the best method?
@Asif Ali Muhammad - the error is bound to the password field, but no, this does not say that the username is correct. All it does is acknowledge that the username and password are not empty, which the user (or cracker) knows already.