ciro ciro - 1 year ago 99
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:

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


The problem is in the error code which returns no message

It returns
callbackValue
which is the value of the constant
INVALID_VALUE
of the
Callback
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;

.....

array(
    '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