0

ZF 1.11.2
I've tried most of the syntaxes. They didn't click.

$validators = array('product_name' => array('alnum'));
//...
$input = new Zend_Filter_Input($filters, $validators, $_POST);

How in the world do you set a custom error message for alnum with the syntax above? Using 'messages' => array('Not alnum!!')? Yeah, well... How? I must've tried 100 nested arrays.

4 Answers 4

2

Use the built in translator.

For example, configure the translator in your config file to use a simple array

; Translations
resources.translate.data = APPLICATION_PATH "/lang"
resources.translate.adapter = "Array"
resources.translate.options.scan = "directory"
resources.translate.options.disableNotices = "1"

This tells the Translate application resource plugin that you want to

  • keep your translations under APPLICATION_PATH/lang
  • use the Array adapter (simplest)
  • scan the translation directory for languages / locales
  • ignore errors about unknown translations (ie user preferes en_AU but you don't have a specific translation file for that language)

Now, create folders for any languages you want to support. At a minimum, you'll want application/lang/en. For example

application
    lang
        en
        en_AU
        en_US

In each language folder, create a translate.php file. This file will contain (and return) an array of key / value pairs for each translation. You can find the keys for each validator message in the validator class. Here's an example for the Alnum validator

<?php
// application/lang/en/translate.php

return array(
    Zend_Validate_Alnum::NOT_ALNUM => 'Not alnum!!',
    Zend_Validate_Alnum::INVALID   => 'Not valid!!'
);

For all Zend validators, you can also use the %value% placeholder in your message, eg

Zend_Validate_Alnum::NOT_ALNUM => "'%value%' is not alpha-numeric"
Sign up to request clarification or add additional context in comments.

3 Comments

+1 Cool! I didn't know this. But couldn't this be problematic if two different validators used the same value for a message key?
@David I think Zend has taken care to avoid conflicts. If you look in the classes, message keys are either prefixed or suffixed with the class name, eg Zend_Validate_Alnum::INVALID == 'alnumInvalid'
Aaah, gotcha. Yep, those Zend boyz are pretty clever. So I would just need to be sure that any externally developed validators that purport to be Zend_Translate aware need to follow some kind of similar 'namespace' scheme. Thanks! ;-)
2

If you are simply trying to change the validation messages for a form element, I have always done it like this (inside a class that extends Zend_Form):

$this->addElement('text', 'myTextField', array(
    'label' => 'The Label',
    'description' => 'The description for the field...',
    'filters' => array(
        'StringTrim',
        // etc
    ),
    'validators' => array(
        array('NotEmpty', true, array(
            'messages' => 'This field is required',
        )),
        array('AnotherValidator', true, array(
            'messages' => 'Bad value',
        )),
    // etc
    ),
));

Are you saying that this didn't work? Or are you using your validator in a more general context, in which case @Phil Brown's (awesome!) answer will do the job.

3 Comments

No, I'm saying that I would like to keep it in a single array in $validators, the 'messages' key overwriting all the template messages. Although @Phil Brown's suggestion is good, I won't use it right now. Something revolving @Maxence's answer, but without instantiation of every new validator.
Aaah, I think I see. For a given element, you want a single message template - something like "Invalid value" - to be used for all the validators on that element? In that case, I imagine that you could create a custom decorator to replace the standard Error decorator. On failed validation, rather than rendering all the messages that come from the validators, you simply render your desired single message. Whaddya think?
[Rephrasing my previous comment slightly so it more closely reflects what I intended] Aaah, I think I see. For a given element, you want a single error message template - something like "Invalid value" - to be used in case any of the validators fail on that element? In that case, I imagine that you could create a custom decorator to replace the standard Error decorator. On failed validation, rather than rendering all the messages that the individual validators attached to the element, you simply render your desired single message. Whaddya think?
2

Disabling the translator on the element will disable the translation of all the validator messages. It is not possible to use a translator on the form or element and overwrite just one validator message. When the element is validated the translator is injected to every validator. The validator will use the translator if it is set. Thereby the custom error message won't be used.

Zend_Validate_Abstract::_createMessage()

// $message is your custom error message
$message = $this->_messageTemplates[$messageKey];

if (null !== ($translator = $this->getTranslator())) {
    // your custom error message gets overwritten because the messageKey can be translated
    if ($translator->isTranslated($messageKey)) {
        $message = $translator->translate($messageKey);
    } else {
        $message = $translator->translate($message);
    }
}

I think it is only possible to use a custom error message by disable the translator on the element.

$element->setDisableTranslator(true)

Comments

0

Use setMessage and disable translator if you have one.

$alnum = new Zend_Validate_Alnum();
$alnum->setDisableTranslator(true);
$alnum->setMessage(
    'Not alnum!!', 
     Zend_Validate_Alnum::NOT_ALNUM
);
$validators = array('product_name' => array($alnum));

If you use your validator on a form element, you have to disable the translator on the element.

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.