Search code examples
symfonysilex

Symfony/Silex: Remove prefix and suffix (code [...]) of validation message


I can change the message of the validation, or even translate it using the translation component.

$errors = $app['validator']->validate($email, new Assert\Email(array(
    'message' => 'The email "{{ value }}" is not a valid email.'
)));

But it still adds the field content before the message and some code after it:

foobar : The email "foobar" ist not a valid email. (code c1051bb4-d103-4f74-8988-acbcafc7fdc3)

How do I remove both, so that there's only the plain message?


Solution

  • The return value of the validate method is an list object. When you cast it to a string, a builtin 'toString' method adds prefix and suffix to the error string.

    The solution was to iterate through errors and call the getMessage method:

    foreach ($errors as $error) {
        echo $error->getMessage().'<br>';
    }