ciro ciro - 1 year ago 96
PHP Question

zf2 change error code returned by Callback validator

I successfully use the ZF2

validator but I have a problem with the code that it returns. Here the config that use:

'name' => 'Callback',
'options' => array(
Callback::INVALID_VALUE => 'Error',
'callback' => function ($value,$context=array()) {
if(empty($value) && empty($context['company'])){
$isValid = false;
$isValid = true;
return $isValid;

The problem is in the error code which returns no message

It returns
which is the value of the constant
of the
validation class

Is there a way to change this value through the options?

I would like to avoid writing a custom class for each item that I have to validate.

Answer Source

I tried but this code works totally fine:

use Zend\Validator\Callback;


    'name' => 'Callback',
    'options' => array(
        'messages' => array(
            Callback::INVALID_VALUE => 'My custom message',
            Callback::INVALID_CALLBACK => 'My custom message'
        'callback' => function(){
                return false;

The key of the message is: callbackValue but the message I get is 'My custom message'

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download