Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable translations on Symfony2 callback validation error messages?

I'm early in a project and I've created some basic functionality, including a custom callback validator (validates end date is after start date). I've since started refactoring to enable translation. I've so far had no issues... until I started looking at translating my custom callback validation.

I read a post online that claimed that I could put my translation key value as my error message and Symfony will automatically translate... but this doesn't seem to be the case for me. Can someone tell me how, or provide a link to documentation, to enable translations in my custom validations?

Here's my current validation code with the translation key included:

<?php
namespace CG5\BFG\CoreBundle\Validators;

use Symfony\Component\Validator\ExecutionContext;

class EndDateValidator
{
    static public function isEndDateValid($entity, ExecutionContext $context)
    {
        if ($entity->getEndDate() <= $entity->getStartDate())
            $context->addViolationAtSubPath('endDate', 'validation.invalid.enddate', array(), null);
    }
}
like image 721
Chris Avatar asked Dec 09 '12 07:12

Chris


1 Answers

I got the same problem. Because symfony2 looks in the "validators" Catalogue for those messages, you have to put your custom error message also in that Catalogue. For example under app/Resources/translations/validators.en.yml.

You need to clear the cache for it to work.

like image 89
vincecore Avatar answered Oct 31 '22 11:10

vincecore