开发者

Zend_Form::setAction() uses current controller in Zend Framework

开发者 https://www.devze.com 2023-04-12 08:21 出处:网络
I declare a publisher controller: class PublisherController extends Zend_Controller_Action { public function indexAction()

I declare a publisher controller:

class PublisherController extends Zend_Controller_Action {

    public function indexAction()
    {
        $this->view->form = $this->_getForm();
        $this->render('form');
    }

    public function dataPostAction()
    {
        //@TODO
    }

    protected function _getForm()
    {
        $form = new Zend_Form();            
        $form->setAction('publisher/dataPost')//Here, I DO NOT want to do: setAction('*/dataPost') with `*` means current controller.
             ->setMethod('post')
             ->setAttrib('id','publisher-form');

        $form->addEle开发者_如何学Goment('text', 'name',
            array(
                'label'=>'First Name',
                'class'=>'required'
            )
        );        
        $form->addElement('submit', 'Save');        
        return $form;
    }
}

Look at the line: $form->setAction('publisher/dataPost')

This means that I want to set the action for the form after submitting is dataPost of publisher controller.

Now I want do do $form->setAction('*/dataPost') with the * means current controller. Because current controller is publisher too.

But it does not work, or am I missing something? Can you tell me what is correct?


publisher/dataPost is much easier to type than $form->setAction($this->getRequest()->getControllerName().'/dataPost'), so I would recommend you stick with what you are already doing.


I don't normally use it in the zend form its self, but preferably in the actions view when calling the form using this code.

<?php echo $this->setAction(url(/.../.../) );?>

This is to echo the form you using in your action and also to set its action. So in short just stick to what you have or use sam's method but I think its better if you have something ACCURATE and working.


I decide the comment of @Sam is the best answer for me.

Using the statement:

$form->setAction($this->getRequest()->getControllerName().'/dataPost')


UPDATE

From this question, I know that: "DO NOT be complex", and some guys here is don't agree so much with the above solution. Now I got this too. But, for this answer, I believe that I SHOULD check and confirm what is correct.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号