zendframework 2 inputfilter自定义默认错误消息

时间:2012-10-04 09:51:09

标签: zend-framework2

我正在尝试自定义默认错误消息“Value is required and can't be empty” 在zf2

我正在使用以下代码在inputfilter

的验证器中添加自定义默认错误消息
$inputFilter->add($factory->createInput(array(
                'name' => 'username',
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name' => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min' => 4,
                            'max' => 20,
                            'messages' => array(
                                'isEmpty' => 'Please enter User Name between 4 to 20 character!' 
                            ),                            
                        ),
                    ),
                ),
            )));

但我收到了以下错误。

Zend\Validator\Exception\InvalidArgumentException

File:
    /home/website/vendor/zendframework/zendframework/library/Zend/Validator/AbstractValidator.php:220

Message:
    No message template exists for key 'isEmpty'

我做错了什么?

reference

3 个答案:

答案 0 :(得分:22)

试试这个

$inputFilter->add($factory->createInput(array(
                'name' => 'username',
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                      'name' =>'NotEmpty', 
                        'options' => array(
                            'messages' => array(
                                \Zend\Validator\NotEmpty::IS_EMPTY => 'Please enter User Name!' 
                            ),
                        ),
                    ),
                    array(
                        'name' => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min' => 4,
                            'max' => 20,
                            'messages' => array(
                                'stringLengthTooShort' => 'Please enter User Name between 4 to 20 character!', 
                                'stringLengthTooLong' => 'Please enter User Name between 4 to 20 character!' 
                            ),
                        ),
                    ),
                ),
            )));

reference

Other validator set

答案 1 :(得分:3)

您还可以设置inputFilter的错误消息:

    $form = new ParticipantForm();  
    $mailInput = new Input('mail');
    $mailInput->setRequired(true);
    $mailInput->setErrorMessage("Empty input");

答案 2 :(得分:1)

StringLength验证器不检查输入是否为空。它检查长度。 StringLength验证器存在以下消息模板:

const INVALID   = 'stringLengthInvalid';
const TOO_SHORT = 'stringLengthTooShort';
const TOO_LONG  = 'stringLengthTooLong';

/**
 * @var array
 */
protected $messageTemplates = array(
    self::INVALID   => "Invalid type given. String expected",
    self::TOO_SHORT => "The input is less than %min% characters long",
    self::TOO_LONG  => "The input is more than %max% characters long",
);

有关直接方法,请参阅@Developer的示例。虽然我建议使用基于CamelCased的验证器命名,但'name' => 'NotEmpty'代替'name' => 'not_empty'

如果您看到每个验证程序类的代码,则可以检查哪些messageTemplates存在。您可以在./vendor/zendframework/zendframework/library/Zend/Validator/*

下找到它们