【问题标题】:Validation based on another field基于另一个字段的验证
【发布时间】:2014-10-10 06:04:47
【问题描述】:

我是这个Symfony 框架的新手,在实施过程中遇到了死胡同。仅当输入用户的current password 时,我才需要验证new passwordconfirm 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;
      }

    }

【问题讨论】:

  • 我能看看你是如何创建表单(so che $this-&gt;createForm())方法的
  • @DonCallisto 谢谢你的回复,我创建了$form = $this-&gt;createForm(new MyAccountForm(), $myAccountUser, array('action'=&gt;$this-&gt;generateUrl('accounts_myaccount')));这样的表格
  • 那么,您能否在问题中包含$myAccountUser 类?有了一点“运气”,我想我已经得到了答案
  • @DonCallisto 哦,我会的……抱歉耽搁了
  • @DonCallisto 我已经添加了用户实体

标签: php forms validation symfony symfony-2.5


【解决方案1】:

如下修改你的类

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()表单的方法时,那段sn-p代码会自动调用一个回调方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-06
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    相关资源
    最近更新 更多