简体   繁体   English

表单中的一个字段未在Symfony3中验证

[英]One field in form doesn't validate in Symfony3

The problem is that "confirmPassword" never pass through validation. 问题是“ confirmPassword”从不通过验证。 Yes, there are also all getters and setters. 是的,也有所有的获取器和设置器。

The "test" and "password" variables validate OK. “ test”和“ password”变量验证OK。

I cannot see a difference between those variables, except that test is of type text and confirmPassword if of type password. 我看不到这些变量之间的区别,只不过测试的类型是文本,如果类型是密码,则是ConfirmPassword。

The message is "This value should not be blank" even when I do enter text into this field. 即使我确实在此字段中输入文本,也会出现消息“此值不能为空”。

What is wrong here? 怎么了

/////////////////////User entity class (part of): ////////////////////用户实体类(的一部分):

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

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

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

//////////////////////////////////user type class: /////////////////////////////////用户类型类别:

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)
        ;
    }
}

//////////////////And finally, the submit function: ////////////////最后,提交功能:

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()
    ]);
}

Edit : added getters and setters. 编辑 :添加了getter和setter。 Please notice that a vertical scrollbar appeared. 请注意,出现了垂直滚动条。

///////////getters and setters /////////// 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;
    }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM