This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » support » General discussion » Newbie: Form validator
Newbie: Form validator [message #97213] Tue, 13 April 2010 00:15 Go to next message
forestunio  is currently offline forestunio
Messages: 2
Registered: April 2010
Location: Poland
Junior Member
hi all,
i'm trying to create login form validation, but i can't deal with it. Here is my form class
class LoginForm extends sfForm {

    public function configure() {
        $this->setWidgets(array(
            'email' => new sfWidgetFormInputText(),
            'password' => new sfWidgetFormInputPassword()
        ));
        $this->widgetSchema->setLabels(array(
            'email' => 'E-mail',
            'password' => 'Password'
        ));
        

        $this->setValidators(array(
            'email' => new sfValidatorEmail(),
            'password' => new myPasswordValidator()
            )
        );      
    }
}


... and here is my validator

class myPasswordValidator extends sfValidatorBase {

    protected function configure($options = array(), $messages = array()) {
        parent::configure($options, $messages);
    }

    protected function doClean($pass) {
        
        if($email == 'test@test.pl' and $pass=='test') {
            return $value;
        } else {
            throw sfValidatorError('Login incorrect');
        }

    }

}


is there any chance to provide to doClean method $email value.
thanks for respond.
Re: Newbie: Form validator [message #97215 is a reply to message #97213 ] Tue, 13 April 2010 01:52 Go to previous messageGo to next message
hsostock  is currently offline hsostock
Messages: 96
Registered: November 2009
Member
Hi forestunio,

When a validator for a field requires a value from another field, you want to use a PostValidator, which performs validation after all the other validators have executed, and which is passed an array of all of the values from the form.

In your case, you would use $this->validatorSchema->setPostValidator(new MyPasswordValidator()) instead of the validator for the password field. Then, in your MyPasswordValidator class, your doClean method would look like so:

<?php
class MyPasswordValidator extends sfValidatorBase
{
  ...
  
  public function 
doClean($values)
  {
    
// note: string comparisons should be performed using  === instead of ==
    
if ($values['email'] === 'test@test.pl' && $values['password'] === 'test')
    {
      return 
$values;
    }
    else
    {
      throw new 
sfValidatorError('Login incorrect');
    }
  }
}
?>


Cheers,

Hale

p.s. There's a good article which goes further into depth about this here. Although it is for Symfony 1.1, it's still a good read (and uses password validation as an example).

[Updated on: Tue, 13 April 2010 01:55]

Re: Newbie: Form validator [message #97242 is a reply to message #97215 ] Tue, 13 April 2010 09:13 Go to previous message
forestunio  is currently offline forestunio
Messages: 2
Registered: April 2010
Location: Poland
Junior Member
thanks a lot
Previous Topic:set password using mysql password function
Next Topic:symfony session problem with doamin name
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software