10

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

我正在使用以下代码在 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'

我做错了什么?

参考

4

3 回答 3

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!' 
                            ),
                        ),
                    ),
                ),
            )));

参考

其他验证器集

于 2012-10-04T10:14:33.240 回答
3

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

    $form = new ParticipantForm();  
    $mailInput = new Input('mail');
    $mailInput->setRequired(true);
    $mailInput->setErrorMessage("Empty input");
于 2012-11-10T12:44:13.873 回答
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/*

于 2012-10-04T11:07:55.847 回答