1

I need to return custom error messages from form validation instead of Symfony's default 'This values is not valid'. I tried a few things on the validation.yml file with properties but could not accomplish this. Has anyone successfully done this?

Thanks

1 Answer 1

2

Constraints usually have the message option. You would use it like this:

Acme\BlogBundle\Entity\Author:
    properties:
        firstName:
            - NotNull:
                message: Whatever!

Update

In the case of a choice constraint, there's more than one message option (documentation).

For example, if the multiple option is set to true you would use multipleMessage:

Acme\BlogBundle\Entity\Author:
    properties:
        styles:
            - Choice: { choices: [novel, haiku], multiple: true, multipleMessage: Whatever! }
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah I tried that but it did not work for whatever reason. The validation just ignored it. I was trying to do it for a 'choice' type. I had to add it in the form itself using the 'invalid_message' attribute.
The Valid constraint unfortunately does not have the message option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.