Search code examples
phpformssymfonyfosuserbundle

Symfony 3.4 + FOSUserBundle add Error-Message for new custom Field


I have created a new custom field in the UserEntity and therefore an Assert-Validation with a error message. This Message can be displayed with {{ form_errors(form) }} globally but I haven´t found a way to display the error-message linked to the field without overriding the whole form_theme.

{{ form_errors(form.field_name) }} does not work either


Solution

  • The FOSUserBundle mapps the errors by himself.

    My Problem was a typo in the field name in the FormType. It should exactly match the variable name in the Entity

    Entity

    /**
     * Some Comment
     *
     * @ORM\Column(name="field_name", type="array", nullable=true)
     * @Assert\NotBlank(message = "Please select at least one field_name")
     */
    protected $fieldName; // Entity variable name
    

    FormType

        $builder->add(
            'fieldName', // Needs to match Entity variable name
            ChoiceType::class,
            array(
                'choices' => array(
                    'Some Choice' => '1',
                    'Other Choice' => '2',
                    '3rd Choice' => '3',
                ),
                'label' => 'form.register.fieldname',
                'translation_domain' => 'user',
                'multiple' => true,
                'expanded' => true,
                'required' => true,
            )
        );