Symfony sfForm密码(必需并进行比较)


Symfony sfForm password (required and compare)

在符号中。我的表格看起来像这个

<?php
class ChangeMyPasswordForm extends sfForm {
    const PASSWORD_REQUIRED_MSG = "We could not update your password. Your password can not be blank. Please enter at least one character for a password.";
    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::IDENTICAL, 
                'confirm', array(), array(
                    'invalid'=>'Passwords do not match. Please try again.'
                )
            )
        );
        $this->mergePostValidator(new sfValidatorString());
        $this->widgetSchema->setLabels(self::$labels);
    }
}

在我的控制器中

public function executeMyAccountPassword(sfRequest $request) {
      $this->form = new ChangeMyPasswordForm();
      $this->validated=$request->getParameter('validated');
      $this->form->bind(array(
              'password' => $request->getParameter('password'),
              'confirm'  => $request->getParameter('confirm'),
      ));
      if ($request->isMethod('post')) {
          if ($this->form->isValid()) {
              $this->validated = true;
              // get player object
              $player = $this->getUser()->getProfile();
              $player->setPassword($request->getParameter('password'));
          }
      }

我正在尝试添加一个验证器,因此密码字段不能为空。我试着修改

$this->setValidators(array('password'=>new sfValidatorString('required'=>'Please enter a password')));

但是,即使数据没有公布,表单也会出现错误。有人能给我展示一个正确的例子,说明如何验证两个匹配的密码字段,并确保字段不为空(在表单提交后)。谢谢

您有第二个错误。将绑定向下移动几行,在if下。这意味着您应该使用发布的值进行绑定

if ($request->isMethod('post')) 
{
  $this->form->bind(array(
    'password' => $request->getParameter('password'),
    'confirm'  => $request->getParameter('confirm'),
  ));
  ... etc...
}

$this-setValidators上有语法错误。你少了几个括号:

$this->setValidators(array('password'=>new sfValidatorString('required'=>'Please enter a password')));