How to inject a Translator in a validator

expressive
zend-validator

#1

Hi, my objective is to inject the translator in a validator, but this is the error that is displayed:

Argument 1 passed to Zend\\Validator\\AbstractValidator::setDefaultTranslator() must be an instance of Zend\\Validator\\Translator\\TranslatorInterface or null, instance of Zend\\I18n\\Translator\\Translator given

After reading https://github.com/zendframework/zend-validator/issues/48 I found that I can’t use the class Zend\I18n\Translator\Translator to translate the validation messages. I don’t like the idea to use the Zend\Mvc dependency so I will create my specific bridge.

My bridge class:

use Zend\I18n\Translator\Translator;
use Zend\Validator\Translator\TranslatorInterface;

/**
 * Bridge class to use the Zend\I18n\Translator\Translator when translating the validation messages
 */
class ValidatorTranslator extends Translator implements TranslatorInterface{

}

#2

Hey @dominicdettabp, what is that you are asking, precisely?


#3

Well, I only wanted a confirm from some other more experts developer than me if this is a good solution.


#4

Ok, I found a problem with this solution how to factory the bridge class?

I tried to use the \Zend\I18n\Translator\TranslatorServiceFactory but naturally it returns the \Zend\I18n\Translator\Translator

The only way to this is to build the bridge class in a new custom factory that does the same of the \Zend\I18n\Translator\TranslatorServiceFactory