我有更改密碼的一種形式,它會檢查password
領域min_length
和required
。然後它將password
字段與confirm
字段進行比較。的Symfony驗證處理
如果我張貼到這種形式具有小於所需的最小字符(和密碼,並確認框匹配),它會因最小長度(預計)和密碼不匹配(意外)錯誤。
誰能告訴爲什麼?
<?php
class ChangeMyPasswordForm extends sfForm {
const PASSWORD_MIN_LENGTH_MSG = "<li>We could not update your password because your password must be at least 8 characters long. Please choose a different password.</li>";
const PASSWORD_MISMATCH_MSG = "<li>We could not update your password because your passwords didn't match. Please try again.</li>";
const PASSWORD_REQUIRED_MSG = "<li>We could not update your password because your password was blank. Please enter a password.</li>";
protected static $labels = array(
'password' => 'Your Password',
'confirm' => 'Re-enter Password',
);
/**
* Called in sfForm's constructor.
*/
public function configure()
{
$this->setWidgets(array(
'password' => new sfWidgetFormInputPassword(array()),
'confirm' => new sfWidgetFormInputPassword(array()),
));
$this->setValidators(array(
'password' => $this->_setPasswordValidator(),
'confirm' => new sfValidatorPass(),
));
$this->mergePostValidator(
new sfValidatorSchemaCompare('password', sfValidatorSchemaCompare::EQUAL,
'confirm', array(), array(
'invalid' => self::PASSWORD_MISMATCH_MSG,
)
)
);
$this->widgetSchema->setLabels(self::$labels);
}
/**
* Returns validators for the password field.
* @return sfValidatorString
*/
private function _setPasswordValidator()
{
$v = new sfValidatorString();
$v->setOption('min_length', 8);
$v->setMessage('min_length', self::PASSWORD_MIN_LENGTH_MSG);
$v->setMessage('required', self::PASSWORD_REQUIRED_MSG);
return $v;
}
}
編輯:這可能有助於=>認爲在那裏我拋出錯誤
<?php if ($form->hasGlobalErrors() || $form->hasErrors()): ?>
<div class="error">
<p class="bottom"><b>Oops!</b></p>
<ul>
<?php foreach ($form->getGlobalErrors() as $name => $error): ?>
<?php echo $error ?>
<?php endforeach; ?>
<?php foreach ($form->getErrorSchema()->getErrors() as $error): ?>
<?php echo $error; ?>
<?php endforeach; ?>
</ul>
</div>
<? endif; ?>
它有點模糊。 。 。你能多解釋一下,還是指點我一篇文章? – TuK 2012-01-12 16:53:39
希望它更清楚一點! – guiman 2012-01-12 17:05:23
感謝噸吉爾曼! http://www.rankmycode.com.ar/code/symfony-registration-form-widget-configuration是我希望看到的! – TuK 2012-01-12 17:34:13