2

This is the link to my form window

Can anybody suggest how to customize the error messages in my form window like - 'This value is not valid', 'Passwords do not match' to appear like a pop up error message for empty field?

Rajesh Vasani
  • 297
  • 4
  • 6
  • 14

4 Answers4

5

Validation error messages can be customized at your bundles validation.yml file. I have added an example.

# src/Vendor/YourBundleName/Resources/config/validation.yml
Vendor\YourBundleName\Entity\YourModel:
    properties:
        email:
            - Email:
                message: The email you entered is not a valid email.
            - NotBlank
                message: Please enter email.

Please read validation doc section and validation constraint reference carefully.

Mun Mun Das
  • 14,992
  • 2
  • 44
  • 43
  • What if I have a form which does not consist of ant entity? – Ali Hassan Dec 03 '13 at 07:59
  • If you are familiarized with the form window image then you would have guessed that the form is a registration form bound to User entity. That's why my answer was related to entity. For forms not bound to entity refer to documentation or search around Google or post a question in SO if you don't find any satisfactory answer. – Mun Mun Das Dec 03 '13 at 13:11
  • I have searched but could not find any satisfactory answer :( Thanks any wyz – Ali Hassan Dec 04 '13 at 07:22
  • Here is a hint, search for "callback validator" – Mun Mun Das Dec 04 '13 at 08:47
  • Thanks I will surely look for it. – Ali Hassan Dec 04 '13 at 12:43
  • @m2mdas , as I can add those messages to the form type validation with html5 validation – jcarlosweb Mar 19 '14 at 09:30
  • @webyseo, to mimic client side validation with Symfony2 server side validation checkout [jsFormValidatorBundle](https://github.com/formapro/JsFormValidatorBundle). Although it does not touch html5 validation message, adding this feature should not be hard as the bundle is customizable. – Mun Mun Das Mar 19 '14 at 17:00
3

It's not symfony but the browser's implementation of the "required" attribute which display these ugly alert messages. Looks like we can't customize this attribute with html/css only but you can try to play around with jquery to customize the error messages before htm5 catch the user input.

Someone asked something similar, hope it can help you: override css for html5 form validation/required popup

Community
  • 1
  • 1
oxidec
  • 318
  • 3
  • 11
  • in symfony2 form validation error how can i show like html5 form validation error. IS there any way in symfony2 so i can show form validation error like empty field error message show when form is submit. – Rajesh Vasani Mar 03 '12 at 06:14
0

I think you search for invalid_message. you can customize your message just like following code block:

$inputsForm = $this->createFormBuilder()
        ->add("networkAddress", TextType::class,[
            'required'=>true
            ])
        ->add("hostsCount",NumberType::class, [
            'required'=>true,
            'invalid_message'=>'my error message!'
        ])
        ->add('calc',SubmitType::class,['label'=>'Berechnen'])
        ->getForm();

DWORD
  • 17
  • 4
0

I think you mean form theme customization, you can check out the official doc:

http://symfony.com/doc/current/cookbook/form/form_customization.html

imikay
  • 742
  • 1
  • 8
  • 17