我是这个Symfony
框架的新手,在实施过程中遇到了死胡同。只有在输入用户的new password
时,我才需要验证confirm password
和current password
字段。
我尽力通过浏览这些链接来理解这个概念,
但事实证明,所使用的类已弃用或需要实体。
这两个字段的实现如下,
//if this field is filled
$builder->add('currentPassword', 'password', array('label'=>'Current Password',
'required'=>false,
'attr'=>array('class'=>'form-control'),
'error_bubbling' => true,
'trim' => true,
'mapped' => false,
'label_attr'=>array('class'=>'col-sm-4 control-label')));
//These repeated fields must be filled or must be set as required
$builder->add( 'password', 'repeated', array( 'type' => 'password',
'required' => false,
'invalid_message' => ErrorMessages::PASSWORDS_DO_NOT_MATCH,
'options' => array('attr' => array('class' => 'password-field form-control')),
'first_options' => array('label' => false,
'error_bubbling' => true,
'label_attr'=>array('class'=>'col-sm-4 control-label')),
'second_options' => array('label' => false,
'label_attr'=>array('class'=>'col-sm-4 control-label'))));
我在控制器中使用了一堆if
条件实现了验证,但是对于像这样的场景学习正确的验证方法会很棒。 :)
谢谢
修改
用户entity
<?php
namespace Proj\Bundle\AccountsBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\User\UserInterface;
use Proj\Bundle\AccountsBundle\Custom\ErrorMessages;
class User implements UserInterface, \Serializable {
/**
* @Assert\Email(message=ErrorMessages::EMAIL_ADDRESS_INVALID)
* @Assert\NotBlank(message=ErrorMessages::EMAIL_ADDRESS_EMPTY)
*/
private $email;
/**
* @Assert\NotBlank(message=ErrorMessages::PASSWORD_EMPTY, groups={"full"})
*/
private $password;
private $oldPassword;
private $id;
private $userId;
private $name;
private $username;
public function __construct() {
}
function setEmail ($email) {
$this->email = $email;
$this->username = $email;
}
function getEmail () {
return $this->email;
}
function setPassword ($password) {
$this->password = $password;
}
function getPassword () {
return $this->password;
}
function setOldPassword ($oldPassword) {
$this->oldPassword = $oldPassword;
}
function getOldPassword () {
return $this->oldPassword;
}
function setId ($id) {
$this->id = $id;
}
function getId () {
return $this->id;
}
function setUserId ($userId) {
$this->userId = $userId;
}
function getUserId () {
return $this->userId;
}
function setName (PersonName $name) {
$this->name = $name;
}
function getName () {
return $this->name;
}
public function eraseCredentials() {
}
public function getRoles() {
return array('ROLE_USER');
}
public function getSalt() {
}
public function getUsername() {
return $this->username;
}
}
答案 0 :(得分:0)
按如下方式修改您的课程
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\ExecutionContext;
/**
*
* @Assert\Callback(methods={"passwordVerify"})
*/
class User implements UserInterface, \Serializable {
//all your old code here
public function passwordVerify(ExecutionContext $context)
{
//your controls about password fields here
//in case of failure you can add that snippet of code
$context->addViolationAtPath($propertyPath,'your message here', array(), null);
}
}
当然,您必须能够访问passwordVerify
函数的所有信息,并且最快的方法是在您的实体中创建字段verifyPassword
,这样当您将表单与实体绑定时所有数据会在那里。
当您使用isValid()
表单的方法时,该代码片段会自动调用回调方法