![](/img/trans.png)
[英]symfony sfValidatorSchemaCompare and dateformat output problem
[英]Symfony sfValidatorSchemaCompare always returns true
在我的形式
<?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; ?>
我无法弄清楚我做错了什么,密码是否匹配并不重要,表单总是返回成功(除非我将比较更改为不相等)。 如何判断值是否回到表单? 我做了什么明显错误的事吗?
您的bind
呼叫已关闭。 它需要一个或两个值,第一个是提交的值,第二个是文件( $request->getFiles()
)。
让我们专注于第一个,因为您没有处理任何文件上传。
它应该是所有表单值的数组在您当前的情况下,您可以这样做(也就是快速n-dirty修复):
$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()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.