Custom error message for input validators (using t

2019-09-14 12:34发布

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条回答
兄弟一词,经得起流年.
2楼-- · 2019-09-14 12:42

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.

查看更多
我命由我不由天
3楼-- · 2019-09-14 12:47

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)
查看更多
Deceive 欺骗
4楼-- · 2019-09-14 12:48

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"
查看更多
女痞
5楼-- · 2019-09-14 12:49

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.

查看更多
登录 后发表回答