0
votes

It's the first time I build a symfony form on our legacy platform (an old monolith we're refactoring more and more with Symfony 5.1). On other Symfony-only platforms I've managed several times to build forms and validations without problems (3.x, 4.x, 5.x).

I stumbled upon a strange behaviour when trying to validate a values length or a valid email address. When I submit empty fields the isValid() method returns true without the NotBlank constraint. For server-side validation I've disabled the client validation.

I'm pretty sure that old code does not interfere; but the fact is, I'm using a php file to which I'm forwarded by a controller.

The $form->isValid() method returns true submitting this field without any data:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', TextType::class, [
            'label' => $this->translator->trans('Your name'),
            'constraints' => [
                new Assert\Length([
                    'min' => 3
                ])
            ]
        ]);
    [snip]    
}

The $form->isValid() method returns false when submitting this field empty - which is the desired result:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', TextType::class, [
            'label' => $this->translator->trans('Your name'),
            'constraints' => [
                new Assert\NotBlank([
                    'message' => $this->translator->trans('This value should not be blank'),
                ]),
                new Assert\Length([
                    'min' => 3,
                    'minMessage' => $this->translator->trans('This value must be at least {{ limit }} characters long')
                ])
            ]
        ]);
    [snip]
}

It seems wrong to me to add a NotBlank constraint - I already check for a minimum length here. Same for other text fields, also the Email constraint returns true when leaving the field empty. I've read the documentation again and again (see https://symfony.com/doc/current/validation.html#constraints-in-form-classes - so it should work), but I don't know what is going wrong here... The fix above was accidentally :). Is someone able to explain, why I need the NotBlank-constraint? Or do I miss something...?

1
This is by design, you can see it in the note just below the usage examplemsg
Thank you. I've overlooked that box -.-manuxi
also, you can remove $this->translator->trans and just leave the translation key, it worksAlexander Br.

1 Answers

0
votes

If you go in the code of the Length validator : https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Validator/Constraints/LengthValidator.php

You can see:

if (null === $value || ('' === $value && $constraint->allowEmptyString)) {
    return;
}

It's considered better that common constraints don't apply to empty values. Let's say the user can optionally submit a phone number. If it's optional, you can't trigger a validation failure on an empty value. So you're free to forbid the empty value with the NotBlank constraint.

It makes less sense on the Length constraint of course, but I assume it's done the same way by harmonization. What if you want to allow empty value, but want that if a value is submitted, it has a minimal length ? Keeping contraints distinct makes the validation more flexible.