TuK
TuK

Reputation: 3556

Symfony sfValidatorSchemaCompare always returns true

in my form

<?php

    class ChangeMyPasswordForm extends sfForm {

        protected static $labels = array(
            'password' => 'Your Password',
            'confirm'  => 'Re-enter Password',
        );

        public function configure()
        {   
            $this->setWidgets(array(
                   'password'  => new sfWidgetFormInputPassword(array()),
                 'confirm' => new sfWidgetFormInputPassword(array()),
            ));

            $this->setValidators(array(
                'password' => new sfValidatorPass(),
                 'confirm' => new sfValidatorPass(),
            ));

            $this->validatorSchema->setOption('allow_extra_fields', true); 

            $this->mergePostValidator(
                new sfValidatorSchemaCompare('password', sfValidatorSchemaCompare::EQUAL, 
                    'confirm', array(), array(
                        'invalid'=>'Passwords do not match. Please try again.'
                    )
                )
            );
            $this->widgetSchema->setLabels(self::$labels);   
        }   
    }

in my controller

public function executeMyAccountPassword(sfRequest $request) {
          $this->form = new ChangeMyPasswordForm();
          $this->validated=$request->getParameter('validated');

          $this->form->bind(array(
                  $request->getParameter('password'),
                  $request->getParameter('confirm'),
          ));

          if ($request->isMethod('post')) {
              if ($this->form->isValid()) {
                  $this->validated = true;                  
                  var_dump($this->form->getErrorSchema());              
              } else {
                  var_dump($this->form->getErrorSchema());              
              }
          }
      }

in my view

<?php if ($validated): ?>
<div class="success">
  <b>Success</b>
</div>
<? endif; ?>

<?php if ($form->hasGlobalErrors() || $form->hasErrors()): ?>
<div class="error">
  <b>FAIL !!</b>
  <ul>
    <?php foreach ($form->getGlobalErrors() as $name => $error): ?>
        <li>
            <?php echo $error ?>
        </li>
    <?php endforeach; ?>

    <?php if($form['password']->hasError()): ?>
        <li>
            <?php echo $form['password']->getError() ?>
        </li>
    <?php endif; ?>

  </ul>
</div>
<? endif; ?>

I cant figure out what I'm doing wrong, it doesn't matter if the passwords match or not, the form always returns success (unless I change the comparison to not equal). How can I tell if the values are making it back to the form ? Am I doing anything obviously wrong ?

Upvotes: 0

Views: 375

Answers (1)

Maerlyn
Maerlyn

Reputation: 34107

Your bind call is off. It needs one or two values, the first one being the submitted values, the second one the files ($request->getFiles()).

Let's focus on the first one, as you're not handling any file uploads.

It should be an array of all the form's values In your current situation, you may do it like this (aka. quick-n-dirty fix):

$this->form->bind(array(
  "password" => $request->getParameter("password"),
  "confirm"  => $request->getParameter("confirm"),
));

On the long term, you should make your form appear as an array in $_POST, add this to your configure():

$this->widgetSchema->setNameFormat("changepasswd[%s]");

This will make your inputs named like changepasswd[password] and changepasswd[confirm]. Binding becomes easy:

$this->form->bind($request->getParameter($this->form->getName()));

Upvotes: 2

Related Questions