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 » symfony 1.3 and 1.4 » [SOLVED]sfValidatorAnd and null objects?
[SOLVED]sfValidatorAnd and null objects? [message #102461] Wed, 14 July 2010 12:27 Go to next message
uorbe001  is currently offline uorbe001
Messages: 3
Registered: July 2010
Junior Member
Hi, I'm having trouble with a form, in this form I want set the postValidator as a sfValidatorAnd with a callback validator and a sfValidatorSchemaCompare. This is the form class:

class ChangePasswordForm extends UserForm {
  public function configure() {
    $this->validatorSchema->setOption('allow_extra_fields', true);

    unset($this['login'], $this['salt'], $this['hashed_password'],
      $this['created_at'], $this['updated_at']);

    $this->widgetSchema['old_pass'] = new sfWidgetFormInputPassword();
      
    $this->validatorSchema['old_pass'] = new sfValidatorString(array(
      'required' => true,'min_length' => 4, 'max_length' => 50));
  
    $this->widgetSchema['password'] = new sfWidgetFormInputPassword();
      
    $this->validatorSchema['password'] = new sfValidatorString(array(
      'required' => true,'min_length' => 4, 'max_length' => 50));
      
    $this->widgetSchema['repeat_password'] = new sfWidgetFormInputPassword();
      
    $this->validatorSchema['repeat_password'] = new sfValidatorString(array(
      'required' => true, 'min_length' => 4, 'max_length' => 50, ));
      
      
    $this->validatorSchema->setPostValidator(new sfValidatorAnd(array(
          new sfValidatorSchemaCompare('password', '==', 'repeat_password', 
            array(), array('invalid' => "The password does not match.") ), 
          new sfValidatorCallback(array('callback' => array($this,
             'checkPassword'))))));
  }
  
  public function checkPassword($validator, $values) {
    if (!empty($values['id'])) {
      $user = Doctrine::getTable('User')->findOneById($values['id']);
      //Check if the user exists.
      if ($user == NULL){
        throw new sfValidatorError($validator, 'This user does not exist');
        return $values;
      }
      //if it exists, check the password
      if (sha1($user->getSalt().$values['old_pass']) != $user
        ->getHashedPassword()) {
        throw new sfValidatorError($validator, 'Invalid old password');
        return $values;
      }
    }
  }
}


When I do this, the validations work fine, but Doctrine complains about a null object *somewhere*.
Warning: Invalid argument supplied for foreach() in 
project/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/form/sfFormDoctrine.class.php 
on line 169

Catchable fatal error: Argument 1 passed to Doctrine_Record::fromArray() must be an array,
 null given, called in 
project/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/form/sfFormDoctrine.class.php
 on line 150 and defined in project/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/
Record.php on line 1957


I've been trying to figure it out, but I don't see what may be wrong, the form works perfectly if I don't set the postValidator (without validation other than the field's validation) and updates the object just fine. What am I missing? Thanks in advance.

[Updated on: Thu, 15 July 2010 12:34]

Re: sfValidatorAnd and null objects? [message #102467 is a reply to message #102461 ] Wed, 14 July 2010 14:32 Go to previous messageGo to next message
xplo  is currently offline xplo
Messages: 428
Registered: September 2008
Faithful Member
i m not sure but i saw 2 thing that could help.
.i think a return just after a throw new exception wont ever be executed.
.you do not alawys return $values in your postvalidator if i read this right
Re: sfValidatorAnd and null objects? [message #102522 is a reply to message #102467 ] Thu, 15 July 2010 12:33 Go to previous message
uorbe001  is currently offline uorbe001
Messages: 3
Registered: July 2010
Junior Member
xplo wrote on Wed, 14 July 2010 14:32

i m not sure but i saw 2 thing that could help.
.i think a return just after a throw new exception wont ever be executed.
.you do not alawys return $values in your postvalidator if i read this right


You are right about the return after the throw, it's useless and it's just the remaining of a previous function.

I just modified the class so it returns $values always, and it works, so that was the issue. Thanks for pointing it out Smile
Previous Topic:generator.yml file tutorial for propel admin generator
Next Topic:How can I check if a file exist or not?
Goto Forum:
  

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