【问题标题】:One field in form doesn't validate in Symfony3表单中的一个字段在 Symfony3 中无效
【发布时间】:2016-01-13 11:55:37
【问题描述】:

问题是“confirmPassword”永远不会通过验证。 是的,还有所有的 getter 和 setter。

“测试”和“密码”变量验证正常。

我看不出这些变量之间的区别,除了 test 是 text 类型,并且 confirmPassword if 是 password 类型。

即使我在此字段中输入文本,消息也是“此值不应为空”。

这里有什么问题?

//////////////////用户实体类(部分):

/**
 * @ORM\Column(type="string", length=128)
 * @Assert\NotBlank (groups={"registration"})
 */
private $password;

/**
* @Assert\NotBlank(
*  groups={"registration"},
* )
*/  
private $confirmPassword;

/**
* @Assert\NotBlank(
*  groups={"registration"},
 */
private $test;

////////////////////////////用户类型类:

class Register extends AbstractType
{
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'validation_groups' => array('registration'),
        ));
    }  

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('username',TextType::class, array('error_bubbling'=>false,'label'=>'Login'))
            ->add('email',  TextType::class, array('error_bubbling'=>false, 'label' => 'E-mail'))
            ->add('password',PasswordType::class, array('error_bubbling'=>false,'label' => 'password'))
            ->add('confirmPassword',PasswordType::class, array('error_bubbling'=>false,'label' => 'password confimation')) 
            ->add('test',TextType::class, array('error_bubbling'=>false,'label' => 'test'))
            ->add('save', SubmitType::class)
        ;
    }
}

///////////////最后是提交函数:

public function registerAction(Request $request, $done = 0)
{
    $user = new \AppBundle\Entity\User();

    $form = $this->createForm(Register::class, $user);

    $form->handleRequest($request);

    if ($form->isSubmitted()) {
        $validator = $this->get('validator');

        if($form->isValid()){
            return $this->redirectToRoute('user_register',array('done' => 1));
        }
    }

    return $this->render('front/users/register.html.twig', [
        'form'=>$form->createView(),
        'done'=>$done,
        'errors' => array()
    ]);
}

编辑:添加了 getter 和 setter。请注意出现了一个垂直滚动条。

//////////getter 和 setter

 public function getUsername() {
        return $this->username;
    }

    public function getSalt() {
        // you *may* need a real salt depending on your encoder
        // see section on salt below
        return $this->salt;
    }

    public function getPassword() {
        return $this->password;
    }

    public function getRoles() {
        $ret_val = array();
        $roles = $this->getUserRoles();

        if($roles) {
            foreach($roles as $Role) {
                $ret_val[] = $Role->getRoleName();
            }
        }
        return $ret_val;
    }

    public function eraseCredentials() {

    }

    public function getConfirmPassword() {
        return $this->password;
    }

    public function setConfirmPassword($password) {
        $this->confirmPassword = $password;
        return $this;
    }  


/**
 * Set password
 *
 * @param string $password
 *
 * @return User
 */
public function setPassword($password) {
    $this->password = $password;

    return $this;
}

/**
 * Set email
 *
 * @param string $email
 *
 * @return User
 */
public function setEmail($email) {
    $this->email = $email;

    return $this;
}



 /**
     * Get email
     *
     * @return string
     */
    public function getEmail() {
        return $this->email;
    }

    public function setTest($test){
        $this->test = $test;
    }

    public function getTest(){
        return $this->test;
    }

【问题讨论】:

  • 能否请您也展示一下您的实体的 getter 和 setter?
  • 使用repeated类型确认第二个密码怎么样?这样 Symfony 将自己比较密码:symfony.com/doc/current/reference/forms/types/repeated.html
  • @xabbuh,嗨,你能看看我更新的问题吗,我添加了 getter 和 setter。
  • @Rvanlaak:是的,我已经看到了,只是我在学习 Symfony 并试图理解问题所在。
  • 至少,getConfirmPassword() 没有返回预期的属性(它返回$password)。

标签: php symfony symfony-2.3


【解决方案1】:

您的getConfirmPassword() 方法没有返回预期的属性(它返回$password)。

【讨论】:

  • 谢谢!正如我所期望的那样,我的声誉太低而无法真正接受,但是 AFAIK,因为我点击了它,当我有更高的声誉时就会发生这种情况。
猜你喜欢
  • 2016-08-24
  • 1970-01-01
  • 2016-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多