4

I would like to add a custom error message to my Callback Validator below ("Zip Code is required" for example), how would I go about doing this?

   $zip = new \Zend\InputFilter\Input('zip');
        $zip->setRequired(false);
        $zip->getValidatorChain()
        ->attach(new \Zend\Validator\Callback(function ($value, $context) {
            if($context['location_type_id'] == \Application\Model\ProjectModel::$LOCATION_TYPE_ID_AT_AN_ADDRESS)
            {
                return (isset($value)&&($value!= NULL))? $value: false;
            }
            return true;
        }));

If you need more information, let me know and I will update. Thanks for your help!

Abor

3 Answers 3

10

Just to throw in my two cents, a custom message can also be set via configuration. I often use this when using a factory type approach like so:

'name' => array(
    ...
    'validators' => array(
        new \Zend\Validator\Callback(
            array(
                'messages' => array(\Zend\Validator\Callback::INVALID_VALUE => '%value% can only be Foo'),
                'callback' => function($value){
                    return $value == 'Foo';
                }))
    )
),

This produces a message like "Bar can only be Foo".

Look closely at the \Zend\Validator\Callback::INVALID_VALUE key, this is a constant defined in \Zend\Validator\Callback:

const INVALID_VALUE = 'callbackValue';

Which is used in that class to set the messages used by the validator:

protected $messageTemplates = array(
    self::INVALID_VALUE    => "The input is not valid",
    self::INVALID_CALLBACK => "An exception has been raised within the callback",
);

Which means you can safely use \Zend\Validator\Callback::INVALID_VALUE => 'Custom message'

I'm not sure whether this breaks a coding principle, somebody please correct me if it does.

Sign up to request clarification or add additional context in comments.

Comments

6

You can do it that way :

$callback = new \Zend\Validator\Callback(function ($value) {
        // Your validation logic
    }
);
$callback->setMessage('Zip Code is required');

$zip = new \Zend\InputFilter\Input('zip');
$zip->setRequired(false);
$zip->getValidatorChain()->attach($callback);

1 Comment

Perfect jchampion, I will update the question with the final solution.
0

Thanks to jchampion for his help.

        $zip = new \Zend\InputFilter\Input('zip');
        $zip->setRequired(false);            
        $callback = new \Zend\Validator\Callback(function ($value, $context) {
            if($context['location_type_id'] == \Application\Model\ProjectModel::$LOCATION_TYPE_ID_AT_AN_ADDRESS)
            {
                return (isset($value)&&($value!= NULL))? true: false;
            }
            return true;
        });
        $callback->setMessage('Zip Code is required');
        $zip->getValidatorChain()->attach(new \Zend\Validator\NotEmpty(\Zend\Validator\NotEmpty::NULL));
        $zip->getValidatorChain()->attach($callback);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.