Quisse
Quisse

Reputation: 778

Non-mapped field is valid whilst it is required and empty

I have a field which is non-mapped and required.

$builder->add('termsAndConditions', CheckboxType::class, [
    'required' => true,
    'mapped' => false,
    'attr' => [
        'class' => 'c-custom-option',
    ],
]);

Clientside validation will throw an error when empty, but serverside says it's valid. Currently i do an extra check on form submission $form->isSubmitted() && $form->isValid() && $form->get('termsAndConditions')->getData()==true but the form->isValid() method shouldn't return true in my opinion

Upvotes: 1

Views: 116

Answers (1)

Manolo
Manolo

Reputation: 26370

As you can see in docs:

If true, an HTML5 required attribute will be rendered. The corresponding label will also render with a required class.

This is superficial and independent from validation. At best, if you let Symfony guess your field type, then the value of this option will be guessed from your validation information.

So, as you can see, it's only about client side validation.

Upvotes: 1

Related Questions