I'm creating a validator for a login form, that extends
sfValidatorSchema because it depends on more than one field, in this
case, in the email and password fields, is that ok?

The validator works great, except for one thing, it is validated
always, not matter if the other validators aren't validated, I mean,
I'm using the sfValidatorSchema as a global post validator. And I just
want this validator to validated the data only if the other validators
didn't throw any exception. Can anyone help me to archive that
behavior?

I believe there is something bad with the validator I created, because
I'm using the sfValidatorSchemaCompare the same way for password
comparation at the customer registration form and it only works when
the other validators didn't throw a exception.

This is the form:

<?php

/**
 * 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
('throw_global_error' => true),array(
        'invalid' => "The email doesn't exist or the password is
invalid",
        'pending' => "Your authorization is still pending of
approvation",
        'inactive' => "Your account was suspended")));

    // Name format
    $this->widgetSchema->setNameFormat('login[%s]');
  }

}

and this is the validator:

<?php
/**
 * 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);

    $this->addMessage('pending', 'Your authorization is still pending
of approvation');
    $this->addMessage('inactive', 'Your account was suspended');

    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));
    }

    // Check the user status
    switch ($user->getStatus()) {
        case 'Active':
                // Do nothing, the status is OK
                break;
        case 'Pending':
                $error = new sfValidatorError($this, 'pending', array('email' =>
$email, 'password'   => $password));
                if ($this->getOption('throw_global_error'))
                  throw $error;
                throw new sfValidatorErrorSchema($this, array($this->getOption
('email') => $error));
                break;
        case 'Inactive':
                $error = sfValidatorError($this, 'inactive', array('email' =>
$email, 'password' => $password));
        if ($this->getOption('throw_global_error'))
          throw $error;
        throw new sfValidatorErrorSchema($this, array($this->getOption
('email') => $error));
                break;
        default:
                // This shouldn't happend, just throw an error
                $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));
                break;
    }

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

Reply via email to