简体   繁体   中英

One field in form doesn't validate in Symfony3

The problem is that "confirmPassword" never pass through validation. Yes, there are also all getters and setters.

The "test" and "password" variables validate OK.

I cannot see a difference between those variables, except that test is of type text and confirmPassword if of type password.

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. Please notice that a vertical scrollbar appeared.

///////////getters and setters

 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 )。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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