在symfony中。 。 。我的形式看起來像這樣Symfony sfForm密碼(必需和比較)
<?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')));
但是,即使沒有發佈數據,表單也會拋出錯誤。任何人都可以向我展示如何驗證兩個匹配密碼字段的正確示例,並確保字段不會留空(表單提交後)。謝謝 !!
的結合? 1.4? – MrGlass 2012-01-11 18:45:24
另外,你得到了什麼錯誤? – MrGlass 2012-01-11 18:45:49
Yessir。 。 。 1.4沒有收到錯誤。它會呈現錯誤消息「密碼不匹配」的表單。請再試一次。'這是正確的信息,我只需要它在表單發佈後運行驗證器。 – TuK 2012-01-11 18:48:23