Zend Framework:设置一个Zend_Form_Element表单字段是必需的,如何更改用于确保元素不为空的验证器

时间:2022-10-10 12:10:38

When using a Zend_Form, the only way to validate that a input is not left blank is to do

使用Zend_Form时,验证输入不是空白的唯一方法就是这样做

$element->setRequired(true);

If this is not set and the element is blank, it appears to me that validation is not run on the element.

如果未设置此元素并且元素为空,则在我看来验证不会在元素上运行。

If I do use setRequired(), the element is automatically given the standard NotEmpty validator. The thing is that the error message with this validator sucks, "Value is empty, but a non-empty value is required". I want to change this message. At the moment I have done this by changing the Zend_Validate_NotEmpty class, but this is a bit hacky.

如果我使用setRequired(),则会自动为该元素提供标准的NotEmpty验证器。问题是该验证器的错误消息很糟糕,“值为空,但需要非空值”。我想改变这个消息。目前我已经通过更改Zend_Validate_NotEmpty类来完成此操作,但这有点hacky。

I would ideally like to be able to use my own class (derived from Zend_Validate_NotEmpty) to perform the not empty check.

理想情况下,我希望能够使用我自己的类(从Zend_Validate_NotEmpty派生)来执行非空检查。

5 个解决方案

#1


3  

I did it this way (ZF 1.5):

我是这样做的(ZF 1.5):

$name = new Zend_Form_Element_Text('name');
$name->setLabel('Full Name: ')
     ->setRequired(true)
     ->addFilter('StripTags')
     ->addFilter('StringTrim')
     ->addValidator($MyNotEmpty);

so, the addValidator() is the interesting part. The Message is set in an "Errormessage File" (to bundle all custom messages in one file):

所以,addValidator()是有趣的部分。消息在“Errormessage文件”中设置(将所有自定义消息捆绑在一个文件中):

$MyNotEmpty = new Zend_Validate_NotEmpty();
$MyNotEmpty->setMessage($trans->translate('err.IS_EMPTY'),Zend_Validate_NotEmpty::IS_EMPTY);

hope this helps...

希望这可以帮助...

#2


3  

By default, setRequired(true) tells isValid() to add a NonEmpty validation if one doesn't already exist. Since this validation doesn't exist until isValid() is called, you can't set the message.

默认情况下,setRequired(true)告诉isValid()添加NonEmpty验证(如果尚不存在)。由于在调用isValid()之前不存在此验证,因此无法设置消息。

The easiest solution is to simply manually add a NonEmpty validation before isValid() is called and set it's message accordingly.

最简单的解决方案是在调用isValid()之前简单地手动添加NonEmpty验证,并相应地设置它的消息。

$username = new Zend_Form_Element_Text('username');
$username->setRequired(true)
         ->addValidator('NotEmpty', true, array('messages' => array('isEmpty' => 'Empty!')));

#3


2  

Add a NotEmpty validator, and add your own message:

添加NotEmpty验证器,并添加您自己的消息:

// In the form class:
$username = $this->createElement('text', 'username');
$username->setRequired();  // Note that this seems to be required!
$username->addValidator('NotEmpty', true, array(
    'messages' => array(
        'isEmpty' => 'my localized err msg')));

Note that the NotEmpty validator doesn't seem to be triggered unless you've also called setRequired() on the element.

请注意,除非您还在元素上调用了setRequired(),否则似乎不会触发NotEmpty验证器。

In the controller (or wherever), call $form->setTranslator($yourTranslator) to localize the error message when it's printed to the page.

在控制器(或任何地方),调用$ form-> setTranslator($ yourTranslator)以在打印到页面时本地化错误消息。

#4


1  

Change the error message.

更改错误消息。

#5


0  

As far as I can see Changing the error message has no way of changing the message of a specific error. Plus the manual makes it look like like this is a function belonging to Zend_Form, but I get method not found when using it on an instance of Zend_Form.

据我所知,更改错误消息无法更改特定错误的消息。另外手册使它看起来像是一个属于Zend_Form的函数,但是我在Zend_Form的实例上使用它时找不到方法。

And example of the useage would be really great.

用例的例子真的很棒。

#1


3  

I did it this way (ZF 1.5):

我是这样做的(ZF 1.5):

$name = new Zend_Form_Element_Text('name');
$name->setLabel('Full Name: ')
     ->setRequired(true)
     ->addFilter('StripTags')
     ->addFilter('StringTrim')
     ->addValidator($MyNotEmpty);

so, the addValidator() is the interesting part. The Message is set in an "Errormessage File" (to bundle all custom messages in one file):

所以,addValidator()是有趣的部分。消息在“Errormessage文件”中设置(将所有自定义消息捆绑在一个文件中):

$MyNotEmpty = new Zend_Validate_NotEmpty();
$MyNotEmpty->setMessage($trans->translate('err.IS_EMPTY'),Zend_Validate_NotEmpty::IS_EMPTY);

hope this helps...

希望这可以帮助...

#2


3  

By default, setRequired(true) tells isValid() to add a NonEmpty validation if one doesn't already exist. Since this validation doesn't exist until isValid() is called, you can't set the message.

默认情况下,setRequired(true)告诉isValid()添加NonEmpty验证(如果尚不存在)。由于在调用isValid()之前不存在此验证,因此无法设置消息。

The easiest solution is to simply manually add a NonEmpty validation before isValid() is called and set it's message accordingly.

最简单的解决方案是在调用isValid()之前简单地手动添加NonEmpty验证,并相应地设置它的消息。

$username = new Zend_Form_Element_Text('username');
$username->setRequired(true)
         ->addValidator('NotEmpty', true, array('messages' => array('isEmpty' => 'Empty!')));

#3


2  

Add a NotEmpty validator, and add your own message:

添加NotEmpty验证器,并添加您自己的消息:

// In the form class:
$username = $this->createElement('text', 'username');
$username->setRequired();  // Note that this seems to be required!
$username->addValidator('NotEmpty', true, array(
    'messages' => array(
        'isEmpty' => 'my localized err msg')));

Note that the NotEmpty validator doesn't seem to be triggered unless you've also called setRequired() on the element.

请注意,除非您还在元素上调用了setRequired(),否则似乎不会触发NotEmpty验证器。

In the controller (or wherever), call $form->setTranslator($yourTranslator) to localize the error message when it's printed to the page.

在控制器(或任何地方),调用$ form-> setTranslator($ yourTranslator)以在打印到页面时本地化错误消息。

#4


1  

Change the error message.

更改错误消息。

#5


0  

As far as I can see Changing the error message has no way of changing the message of a specific error. Plus the manual makes it look like like this is a function belonging to Zend_Form, but I get method not found when using it on an instance of Zend_Form.

据我所知,更改错误消息无法更改特定错误的消息。另外手册使它看起来像是一个属于Zend_Form的函数,但是我在Zend_Form的实例上使用它时找不到方法。

And example of the useage would be really great.

用例的例子真的很棒。