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);
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With