本文介绍了ZF:如何检查GET请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格。有两个选择框不能是0:field1,field2。
如果我设置POST方法,那么它工作正常。

 $ this-> view-> searchForm = new Default_Form_Parameters(); 
$ data = $ this-> getRequest() - > getParams();
if($ this-> view-> searchForm-> isValid($ data)){

}

如果我有以下请求,则isValid返回false。没关系。

  http://site.ru/?field1=0&field2=0 

如果我有另一个请求,比如

  http ://site.ru/?crash 

然后isValid返回true。这是错误的。

任何想法是什么问题?



PS这里有一个验证字段: p>

  $ required = new Zend_Validate_NotEmpty(); 
$ required-> setType($ required-> getType()| Zend_Validate_NotEmpty :: INTEGER | Zend_Validate_NotEmpty :: ZERO);

$ input = new Zend_Form_Element_Select('cat');
$ input-> setLabel('theme')
- > addMultiOptions(array('0'=&'; -----------')+ $ categories)
- > addValidators(array($ required));


解决方案

设置 Zend_Validate_NotEmpty 验证程序是不够的。它仅适用于已将值设置为此字段的情况。如果像''这样的空值被设置,它将不会被验证。但是,默认情况下它被设置为 Null 我认为这意味着没有设置值。你必须告诉它它是'presence'=> 'required',或使用 setRequired()


I have a form. There are two selectboxes which are cannot be 0: field1, field2.If I set POST method then it works fine. If GET - wrong.

Here my controllers' part:

$this->view->searchForm = new Default_Form_Parameters();
$data = $this->getRequest()->getParams();
if ($this->view->searchForm->isValid($data)) {

}

If I have following request then isValid returns false. That's ok.

http://site.ru/?field1=0&field2=0

If I have another request like

http://site.ru/?crash

then isValid returns true. That is wrong.

Any ideas whats the problem?

PS here one of fields with validator:

$required = new Zend_Validate_NotEmpty();
$required->setType ($required->getType() | Zend_Validate_NotEmpty::INTEGER | Zend_Validate_NotEmpty::ZERO);

$input = new Zend_Form_Element_Select('cat');
$input->setLabel('theme')
      ->addMultiOptions(array('0' => ' ----------- ') + $categories)
      ->addValidators (array ($required));
解决方案

Setting a Zend_Validate_NotEmpty validator isn't enough. It only applies if a value has been set to this field. If an empty value like '' would be set it wouldn't validate. However, by defaults it's set to Null I think and that means no value has been set. You have to tell it that it that is 'presence' => 'required', or use setRequired().

这篇关于ZF:如何检查GET请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 09:34