Chvanikoff
Chvanikoff

Reputation: 1329

Setting action parameter to form in Zend

Im using "Zend form". What should I do to get correct "action" parameter in my FORM tag?
For example, the form Form_Search used in SearchController in indexAction. Where and how should I set the action parameter to form to make it "/my/app/directory/search/index"?

Update: "action" parameter should be the same as the form will assume if not set. I just need the param in FORM to simplify JS coding.

Upvotes: 1

Views: 9924

Answers (4)

user6623600
user6623600

Reputation: 9

            `$this->setAttribs(array(
                'accept-charset' => 'utf-8',
                'enctype' => Zend_Form::ENCTYPE_URLENCODED,
                'method' => 'get',
                'action' => '/controller/action/param/attr'
            ))`

Upvotes: 0

David Weinraub
David Weinraub

Reputation: 14184

You can use the url() view helper to create your action urls.

$view->url(array(
    'module'     => 'mymodule',
    'controller' => 'mycontroller',
    'action'     => 'myaction',
    'param1'     -> 'myvalue1',
    // etc
), 'default'); 
// 'default' here refers to the default route. 
// If you specify a custom route, then that route will be used to 
// assemble() the url.

Depending where you call $form->setAction(), you can access to the $view in different ways.

In the controller: $this->view

In the form itself: $this->getView()

In a view script: $this

In view helper extending Zend_View_Helper_Abstract: $this->view

Hope it helps!

Upvotes: 2

Tomáš Fejfar
Tomáš Fejfar

Reputation: 11217

Just an addition to previous answers. Kind of "industry standard approach" is to render/validate/process the form all on the same page. Then there is no need to set the action as it's filled with current URL.

Exception is for example serch field in layout. Then use $form->setAction($url). But always remember to echo the form in the search action. If the form value is invalid, ZF would expect you to render it in page with the errors. Or you can use $form->getErrors().

Upvotes: 1

Marcin
Marcin

Reputation: 238239

You can use setAction method of Zend_Form, e.g.:

$yourForm->setAction('/my/app/directory/search/index');

Hope this is what you are looking for.

EDIT: You can retrieve many info about the request, in an action, using $this->getRequest() method that returns an instance of Zend_Controller_Request_Http. For example, if to get basePath and everything between the BaseUrl and QueryString you can do:

    $infoPath = $this->getRequest()->getPathInfo();
    $basePath = $this->getRequest()->getBaseUrl();

    $yourForm->setAction($basePath . $infoPath);

Zend_Controller_Request_Http has other methods that might be useful to you.

Upvotes: 5

Related Questions