skip to Main Content

Hello I have a Symfony 5 form with constraints for my fields, my constraint for Length works perfectly, but NotBlank not correctly. I have a "type" and "label" field, I’m testing the form and the constraints with "type", if I validate my form with "type" empty, it displays the default browser message "Please fill out this field" whereas I’d like to have my custom message that I’ve defined in my entity for the "NotBlank" constraint.

I think the problem is caused by the "required => true" because if I change it to "false" and add an "empty_data => ” " it works and I can see my custom message but not completely because in my modification form, if I empty the "type" field and validate, it will bug with this message: Expected argument of type "string", "null" given at property path "type".

I’m using Symfony 5, Bootstrap 5, here’s my entity, my twig form and my FormType.php

Entity

/**
     * @ORMColumn(type="string", length=255)
     * @AssertLength(
     *      min = 2,
     *      max = 50,
     *      minMessage = "Le type doit faire au minimum {{ limit }} caractères",
     *      maxMessage = "Le type doit faire au maximum {{ limit }} caractères"
     * )
     * @AssertNotBlank(
     *      message = "Le type est un champ obligatoire"
     * )
     */
    private $type;

Twig form

{{ form_start(form) }}
    <div class="container-card-global mb-5">
        <div class="form-field">
            {{form_label(form.type)}}
            {{form_errors(form.type)}}
            {{form_widget(form.type)}}
        </div>

        <div class="form-field">
            {{form_label(form.description)}}
            {{form_widget(form.description)}}
            {{form_errors(form.description)}}
        </div>

        <div class="form-field">
            {{form_label(form.statut)}}
            {{form_widget(form.statut)}}
        </div>


        <div class="text-center">
            <button class="btn btn-save-global">{{ button_label|default('Valider') }}</button>
        </div>
    </div>
{{ form_end(form) }}

FormType.php

$builder
            ->add('type', TextType::class, [
                'required' => true,
                'label' => 'Type',
            ])

Ty for help !

I’ve looked at some forums, I’ve tried using "novalidate" but it comes back the same, the same error message, I’d really like to keep "required => true" and manage to get my custom message "this field cannot be blank".

2

Answers


  1. You are confusing server side error and front side error.
    When you write required => true it will apply the html attribute required to your input element. https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_required

    This is why you see default brower error. You can inspect your input html tag with F12 and remove the required if you want to try.

    Then. If you manage to submit the form (sending data to server). Symfony will apply its own constraint (your assert).

    This is why your form show your error properly since you used this function on each entry of your form form_errors.

    But in that case, the error you see are returned from the server. They are not browser error.

    Anyway, keeping required => true is good for UX (User experience). And for a complete UX, having only "dynamic" error showing is a good thing. It prevent user to be forced to submit before knowing he made mistake.

    BUT Symfony constraint are also here to PROTECT what someone can submit so you never get incomplete data registered. You can see it as the "wall of china". The last thing that project your project data.

    Login or Signup to reply.
  2. For form classes you need to add the constraint like this:
    https://symfony.com/doc/current/validation.html#constraints-in-form-classes

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search