我有一个具有2个函数的控制器,每个函数都有一个表单被生成并传递给一个视图。验证不适用于控制器中的第二种形式
第一个是针对用户配置文件,并完美地提取Validation.yml文件 - 检查名称和电子邮件是否为空。
在同一个验证文件中,我已将密码设置为不为空。
但是,允许用户更改其密码的第二种形式不想验证。
第二种形式基本上是第一种,但有两个字段“密码”和“确认密码”的副本。
当然,因为我的字段被称为“密码”,它应该匹配我的用户实体中的变量? (这是正在发生的事情对于第一种形式)
一些代码:
passwordAction
public function passwordAction(Request $request)
{
$form = $this->createFormBuilder()
->add('password', null, array('label' => 'New Password'))
->add('confirmPassword', null, array('label' => 'Confirm Password'))
->getForm();
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
// perform some action, such as saving the task to the database
//return $this->redirect($this->generateUrl('dashboard'));
}
}
return $this->render('UserBundle:Dashboard:password.html.twig', array('form' => $form->createView()));
}
}
密码表单枝:(NB - 在NOVALIDATE是rturn关闭HTML5验证!!)
<form action="{{ path('update_password') }}" method="post" {{ form_enctype(form) }} novalidate="novalidate">
<fieldset>
<legend>Change Your Password</legend>
{{ form_row(form.password) }}
{{ form_row(form.confirmPassword) }}
</fieldset>
<fieldset>
<div class="actions">
<input class="btn primary" type="submit" value="Update" />
<input class="btn error" type="reset" value="Reset">
</div>
</fieldset>
</form>
validation.yml:
UserBundle\Entity\Profile:
properties:
name:
- NotBlank: ~
UserBundle\Entity\User:
properties:
email:
- NotBlank: ~
- Email:
message: The email "{{ value }}" is not a valid email.
checkMX: true
password:
- NotBlank: ~
周
的routing.yml:
password:
pattern: /password
defaults: { _controller: UserBundle:Dashboard:password }
update_password:
pattern: /password
defaults: { _controller: UserBundle:Dashboard:password }
requirements:
_method: POST
哈哈,是啊我有点想了很久以后,但非常感谢你回答:) – 2012-02-03 09:21:53