Welcome to Abdul Malik Ikhsan's Blog

Zend Framework 2 : Translate Validator Messages

Posted in Tutorial PHP, Zend Framework 2 by samsonasik on October 15, 2012

Zend Framework 2 Validator classes provide a setTranslator() method with which you can specify an instance of Zend\I18n\Translator\Translator which will translate the messages in case of a validation failure. The getTranslator() method returns the set translator instance. This is very easy in Zend Framework 2.

This is it :
1. Decide validator which we will use

$emailvalidator = new \Zend\Validator\EmailAddress();

2. Prepare the translation file
For example, create a translation file named indonesia.php in YourModule/language folder.

return array(
    'Invalid type given. String expected' => 'Tipe tidak valid, String yang diharapkan'  

3. Add file to Translator

$translator =  new \Zend\I18n\Translator\Translator();

4. Set Validator Translator


5. Testing :

if ( ! $emailvalidator->isvalid(1)){
    foreach($emailvalidator->getMessages() as $key=>$row)
        echo $row;
        echo "<br />";

This is the complete sourcecode :

$translator =  new \Zend\I18n\Translator\Translator();

if ( ! $emailvalidator->isvalid(1)){
    foreach($emailvalidator->getMessages() as $key=>$row)
        echo $row;
        echo "<br />";

If not valid, we will see the following messages :
Tipe tidak valid, String yang diharapkan

For working with form, just add options into validator that registered in inputfilter.

//common code here...
    public function getInputFilter()
        if (!$this->inputFilter) {
            $inputFilter = new InputFilter();
            $factory     = new InputFactory();

            $translator =  new \Zend\I18n\Translator\Translator();
            $translator->addTranslationFile('phparray', './module/YourModule/language/indonesia.php');
                'name'     => 'email',
                'validators' => array(
                        'name'    => 'EmailAddress',
                        'options' => array(
                            'translator' => $translator
            $this->inputFilter = $inputFilter;

        return $this->inputFilter;

Done !

References :
1. zf2.readthedocs.org/en/latest/modules/zend.validator.html


13 Responses

Subscribe to comments with RSS.

  1. zeus0 said, on February 28, 2013 at 10:52 pm

    or if u would like to set all errors for validation add translation file with all errors in onBoostrap in Application Module.php and in head of file put “use Zend\Validator\AbstractValidator” and after set translation file put “AbstractValidator::setDefaultTranslator($translator);”

    In my file whole block of code:

    use Zend\Validator\AbstractValidator;
    class Module {
        public function onBootstrap($e) {
            $translator = $e->getApplication()->getServiceManager()->get('translator');
            $eventManager        = $e->getApplication()->getEventManager();
            $moduleRouteListener = new ModuleRouteListener();

    of course where pl language I have get it from variables, only for clean code I set static string where language.

    Here is documentation with this example: http://framework.zend.com/manual/2.1/en/modules/zend.validator.messages.html

  2. Martin said, on June 19, 2013 at 9:57 pm

    Is it working with version 2.2.. and it’s Zend\Validator\Translator\TranslatorInterface ?

  3. dragos said, on July 4, 2013 at 12:46 am

    I want to change the default message for a select element( that nonempty), i can’t in transalation because i have many fields and every fields have a different message

  4. ezpaider said, on August 13, 2013 at 3:11 am

    @samsonasik, thanks for all your tutorials. They’ve been incredibly helpful. This one however is not working. When adding the $translator to options, the code breaks and gives Error Code 500. The same thing happens when I add AbstractValidator::setDefaultTranslator($translator) if I try it the other way. I am doing this in a second module where I don’t have any translator service manager. Any thoughts?

    • Willing said, on October 10, 2013 at 11:03 am

      Version 2.2 ?

  5. zirtrex said, on December 28, 2013 at 2:09 am

    Hi, I´m using zf 2.2.4 and dont work

  6. zirtrex said, on December 28, 2013 at 2:19 am

    catchable fatal error: must be an instance of ‘Zend\Validator\Translator\TranslatorInterface’, instance of Zend\I18n\Translator\Translator gived.

  7. zirtrex said, on December 28, 2013 at 2:27 am

    resolve it, change: Zend\I18n\Translator\Translator for Zend\Mvc\I18n\Translator

  8. Bart McLeod said, on April 1, 2014 at 4:06 pm

    The link to readthedocs is broken.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: