ZF: how to check GET request?

2019-08-02 06:02发布

问题:

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));

回答1:

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().



回答2:

$data = $this->_request->getParam('getkey');    
if($data)
{
    //do something
}
else{
    throw new Zend_Exception("No GET value");
}


回答3:

This is how I use to work with forms...

public function createAction()
    {
    $form = new Application_Form_PageCreate();
    if ($this->getRequest()->isPost()) {
        if ($form->isValid($this->getRequest()->getPost())) {
             //do what you need
        }
    }
    $this->view->form = $form;
    }

as you can see I use "getPost()" and not "getParams()" but that's in cause of my FORM method