以我的形式
<?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);
}
}
在我的控制器中
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());
}
}
}
在我看来
<?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; ?>
我无法弄清楚我做错了什么,密码是否匹配并不重要,表单总是会返回成功(除非我将比较更改为不相等)。如何判断值是否回到表单?我做了什么明显的错误吗?
答案 0 :(得分:2)
您的bind
电话已关闭。它需要一个或两个值,第一个是提交的值,第二个是文件($request->getFiles()
)。
让我们专注于第一个,因为您没有处理任何文件上传。
它应该是所有表单值的数组在当前情况下,你可以这样做(又名。快速修复):
$this->form->bind(array(
"password" => $request->getParameter("password"),
"confirm" => $request->getParameter("confirm"),
));
从长远来看,您应该在$_POST
中将表单显示为数组,并将其添加到configure()
:
$this->widgetSchema->setNameFormat("changepasswd[%s]");
这会使您的输入命名为changepasswd[password]
和changepasswd[confirm]
。绑定变得容易:
$this->form->bind($request->getParameter($this->form->getName()));