8000 GitHub Β· Where software is built
[go: up one dir, main page]

Skip to content
Form: RepeatedType Field ignores validation if single value is passedΒ #21242
Closed
@HKandulla

Description

@HKandulla
Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 3.2.1

I came across a strange behaviour that looks like a bug to me. We use Symfony for a JSON API and fill the forms with arrays. When I submit an array where the password consists of an array with two keys, the validation workd fine. But when pass an array where password has a single value or even an empty string, the validation is ignored and the form validates.

[
	"password": [
		"first": "string",
		"second": "string"
	]
]

validates according to rules

[
	"password": ""
] 

ignores validation rules and validates to true

FormType

class UserPasswordType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array                $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('password', RepeatedType::class, array(
                'required' => true,
                'type' => PasswordType::class,
                'invalid_message' => 'The password fields must match.',
                ))
        ;
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => User::class,
            'csrf_protection' => false,
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'app_user_password';
    }
}

Action

    public function updatePasswordAction(User $user, Request $request)
    {
        $data = $this->getDataArrayFromRequest($request);
        $form = $this->createForm(UserPasswordType::class, $user);
        $form->submit($data);
        if ($form->isValid()) {
            $this->get('app.entity.user_manager')->updatePassword($user);

            return $this->userJsonResponse($user, 201);
        }

        return $this->formErrorsJsonResponse($form);
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0