I'm creating a validator for a login form, so I was thinking about a
global validator since the validator works with the data from two
fields: email and password.
I believe that a sfValidatorSchema is a validator that works with more
than one field of the form, is that right?
I don't understand very well what's the difference between a
sfValidatorError and sfValidatorErrorSchema, now I have this code, and
sfValidatorError is throw when the validation fails, but I don't
understand the difference between these two exceptions:
// Check if the user exists
if (!$user) {
$error = new sfValidatorError($this, 'invalid', array(
'email' => $email,
'password' => $password
));
if ($this->getOption('throw_global-error'))
throw $error;
throw new sfValidatorErrorSchema($this, array($this->getOption
('email') => $error));
}
BTW, the validator works well, it throws an error when the login
fails, but it only marks the email field as the one that has the
error, and I would love to mark both the email and password field when
the user/password validation fails, I'm posting all the code I did,
could anyone advice me how to archive the behavior I just described?
And lastly, any links/help about the forms validators is welcome (I
already read all the doc that I could find in the symfony web page)
So, this is the code for the login form:
/**
* Login form class
*
*/
class LoginForm extends sfForm
{
public function configure()
{
$this->setWidgets(array(
'email' => new sfWidgetFormInput(),
'password' => new sfWidgetFormInputPassword()
));
$this->setValidators(array(
'email' => new sfValidatorEmail(array('max_length' =>
255)),
'password' => new sfValidatorString(array('max_length' =>
255)),
));
// User/Pass validator
$this->validatorSchema->setPostValidator(new
cmsValidatorSchemaLogin('email', 'password', array(), array('invalid'
=> "The email doesn't exist or the password is invalid")));
// Name format
$this->widgetSchema->setNameFormat('login[%s]');
}
}
And this is the code that I created for the validator:
/**
* cmsValidatorSchemaLogin Validates email/password for the customer
*
*/
class cmsValidatorSchemaLogin extends sfValidatorSchema
{
/**
* Constructor.
*
* Available options:
*
* * email: The email field name
* * password: The password field name
*
* @param string $email The email field name
* @param stirng $password The password field name
* @param array $options An array of options
* @param array $messages An array of error messages
*
* @see sfValidatorBase
*/
public function __construct($email, $password, $options = array(),
$messages = array())
{
$this->addOption('email', $email);
$this->addOption('password', $password);
$this->addOption('throw_global_error', false);
parent::__construct(null, $options, $messages);
}
/**
* @see sfValidatorBase
*/
protected function doClean($values)
{
if (is_null($values))
{
$values = array();
}
if (!is_array($values))
{
throw new InvalidArgumentException('You must pass an array
parameter to the clean() method');
}
$email = isset($values[$this->getOption('email')]) ? $values
[$this-
>getOption('email')] : null;
$password = isset($values[$this->getOption('password')]) ? $values
[$this->getOption('password')] : null;
// Find the email in the newsletter database
$query = Doctrine_Query::create()->
from('Customer')->
where('email = ? and password = ?', array($email, $password));
// Get the user
$user = $query->fetchOne();
// Check if the user exists
if (!$user) {
$error = new sfValidatorError($this, 'invalid', array(
'email' => $email,
'password' => $password
));
if ($this->getOption('throw_global-error'))
throw $error;
throw new sfValidatorErrorSchema($this, array($this->getOption
('email') => $error));
}
return $values;
}
/**
* @see sfValidatorBase
*/
public function asString($indent = 0)
{
$options = $this->getOptionsWithoutDefaults();
$messages = $this->getMessagesWithoutDefaults();
unset($options['email'], $options['password']);
$arguments = '';
if ($options || $messages)
{
$arguments = sprintf('(%s%s)',
$options ? sfYamlInline::dump($options) : ($messages ? '{}' :
''),
$messages ? ', '.sfYamlInline::dump($messages) : ''
);
}
return sprintf('%s %s %s %s',
str_repeat(' ', $indent),
$this->getOption('email'),
$arguments,
$this->getOption('right_field')
);
}
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---