# ZF3 validator php file not working

**URL:** <https://discourse.laminas.dev/t/zf3-validator-php-file-not-working/1323>\
**Category:** Components & MVC\
**Created:** [January 20, 2020, 4:19pm UTC](https://discourse.laminas.dev/t/zf3-validator-php-file-not-working/1323 "2020-01-20T16:19:36Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jilco](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@Jilco](https://discourse.laminas.dev/u/Jilco)\
**Post date:** [January 20, 2020, 4:19pm UTC](https://discourse.laminas.dev/t/zf3-validator-php-file-not-working/1323/1 "2020-01-20T16:19:36Z")

</div>

I translate validation errors with a PHP file and that works fine for eg:

```
 "Invalid type given. String expected" => "Verkeer type opgegevens er wordt tekst verwacht",

```

But when using a wildcard it does not work:

```
 "The input is less than %min% characters long" => "De input is minder dan %min% karakters",

```

What is wrong?

I use this settup:

Global.php

```
 'service_manager' => array(
    'aliases' => array(
        'translator' => 'MvcTranslator',
    ),
 ),

```

module.config.php

```
'translator' => [
    'locale' => 'nl_NL',
    'translation_file_patterns' => [
        [
            'type' => 'phparray',
            'base_dir' => 'module/Application/lang',
            'pattern' => '%s.php',
        ],
    ],
]

```

nl\_NL.php

```
<?php

return [
// Zend\Authentication\Validator\Authentication
"Invalid identity" => "Invalid identity",
"Identity is ambiguous" => "Identity is ambiguous",
"Invalid password" => "Invalid password",
"Authentication failed" => "Authentication failed",
// Zend\Validator\EmailAddress
"Invalid type given. String expected" => "Invalid type given. String expected",
"The input is not a valid email address. Use the basic format local-part@hostname" => "The input is not a valid email address. Use the basic format local-part@hostname",
"'%hostname%' is not a valid hostname for the email address" => "'%hostname%' is not a valid hostname for the email address",

```

…

];

Vallidator in Form

```
$inputFilter->add([
    'name' => 'naam',
    'required' => true,
    'filters' => [
       ['name' => 'StringTrim'],
       ['name' => 'StripTags'],
       ['name' => 'StripNewlines'],
    ],                
    'validators' => [
       [
        'name' => 'StringLength',
          'options' => [
            'min' => 3,
            'max' => 128
          ],
       ],
    ],
  ]
);
```

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [January 20, 2020, 6:01pm UTC](https://discourse.laminas.dev/t/zf3-validator-php-file-not-working/1323/2 "2020-01-20T18:01:06Z")

</div>

Hi @Jilco,  
unfortunately, in your description the explanation of your procedure is missing. Therefore here are two examples.

## Example 1

```php
$validator = new Laminas\Validator\StringLength(['min' => 6]);
$validator->setMessage(
    'De input is minder dan %min% karakters',
    Laminas\Validator\StringLength::TOO_SHORT
);
$validator->isValid('Test');

var_dump($validator->getMessages());

```

Output:

```auto
array(1) {
  'stringLengthTooShort' =>
  string(34) "De input is minder dan 6 karakters"
}

```

## Example 2

This example uses [laminas-i18n-resources](https://docs.laminas.dev/laminas-i18n-resources/) and [laminas-mvc-i18n](https://docs.laminas.dev/laminas-mvc-i18n/) where are all messages already translated:

```php
$translator = new Laminas\I18n\Translator\Translator();
$translator->addTranslationFile(
    Laminas\I18n\Translator\Loader\PhpArray::class,
    __DIR__
    . '/vendor/laminas/laminas-i18n-resources/languages/nl/Laminas_Validate.php'
);
$mvcTranslator = new Laminas\Mvc\I18n\Translator($translator);

$validator = new Laminas\Validator\StringLength(['min' => 6]);
$validator->setTranslator($mvcTranslator);
$validator->isValid('Test');

var_dump($validator->getMessages());

```

Output:

```auto
array(1) {
  'stringLengthTooShort' =>
  string(36) "De input is minder dan 6 tekens lang"
}

```

* * *

I hope this helps otherwise please provide a code example which allows to reproduce your problem.

---

<div class="post-metadata">

**Author:** ![Jilco](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@Jilco](https://discourse.laminas.dev/u/Jilco)\
**Post date:** [January 20, 2020, 6:15pm UTC](https://discourse.laminas.dev/t/zf3-validator-php-file-not-working/1323/3 "2020-01-20T18:15:29Z")

</div>

I added the code example, thanks in advance!

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [January 20, 2020, 7:11pm UTC](https://discourse.laminas.dev/t/zf3-validator-php-file-not-working/1323/4 "2020-01-20T19:11:41Z")

</div>

Does a translation work at all or only the messages without placeholders?

---

<div class="post-metadata">

**Author:** ![Jilco](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@Jilco](https://discourse.laminas.dev/u/Jilco)\
**Post date:** [January 20, 2020, 7:22pm UTC](https://discourse.laminas.dev/t/zf3-validator-php-file-not-working/1323/5 "2020-01-20T19:22:01Z")

</div>

New problem , after

```
composer require laminas/laminas-mvc-i18n

```

My content is gone! Layout loads as it should, but content part is gone

```
<?= $this->content ?>

```

Something with the renaming i think, please help…

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [January 20, 2020, 7:26pm UTC](https://discourse.laminas.dev/t/zf3-validator-php-file-not-working/1323/6 "2020-01-20T19:26:54Z")

</div>

> [@Jilco](#):
>
> Something with the renaming i think, please help…

Please note this is a different problem then the translation of validator messages.  
Follow the [migration guide](https://docs.laminas.dev/migration/) and if you still have problems, please open new thread for this.

Thanks!

---

<div class="post-metadata">

**Author:** ![Jilco](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@Jilco](https://discourse.laminas.dev/u/Jilco)\
**Post date:** [January 21, 2020, 9:07am UTC](https://discourse.laminas.dev/t/zf3-validator-php-file-not-working/1323/7 "2020-01-21T09:07:53Z")

</div>

> [@froschdesign](#):
>
> **DIR**.

I did the migration part this morning with succes 🙂

Is there a possibility to use the migration file as a service or something, so that i don’t have to rebuild al te vallidators?

---

<div class="post-metadata">

**Author:** ![Jilco](https://avatars.discourse-cdn.com/v4/letter/j/a88e57/32.png) [@Jilco](https://discourse.laminas.dev/u/Jilco)\
**Post date:** [January 22, 2020, 7:57pm UTC](https://discourse.laminas.dev/t/zf3-validator-php-file-not-working/1323/8 "2020-01-22T19:57:36Z")

</div>

Stil no succes with the wildcards, normal translation is working but:

```
 "The input is not less than '%max%'" => "De input is niet minder dan '%max%'"

```

Is not translating ☹

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [January 22, 2020, 8:07pm UTC](https://discourse.laminas.dev/t/zf3-validator-php-file-not-working/1323/9 "2020-01-22T20:07:37Z")

</div>

Does it works with the usage of the related class constant?

```php
[
    Laminas\Validator\LessThan::NOT_LESS => "De input is niet minder dan '%max%'",
]

```
