Zend Form SetAction Using Named Routes
Asked Answered
A

4

10

I have a form that I am trying to set the action for. I want to declare the action inside my form file (which extends Zend_Form) instead of in a controller or view, using a route I have created in my bootstrap. Usually when I want to use a route I do something like

$this->url(array(), 'route-name');

in the view, or

$this->_helper->url(array(), 'route-name');

in the controller.

How do I call a route from within Zend_Form?


edit: I have given up trying to load a route into zend_form. Perhaps in a future release there may be a function to easily do this?

I have created a viewScript for my form and set the route in that: In the form init function:

$this->setDecorators(array(
    'PrepareElements',
        array(
            'ViewScript', array(
                    'viewScript' => 'forms/formView.phtml'
            ))));

and in the view file:

<form method="post" action="<?php echo $this->url(array(), 'route-name'); ?>" enctype="application/x-www-form-urlencoded">
    <?php
        foreach ($this->element->getElements() as $element)
        {
            echo $element;
        }
    ?>
</form>
Alcaraz answered 20/7, 2010 at 10:8 Comment(1)
Dont give up so fast. Just give us the time to answer your question ;)Joline
J
18

Method 1: Get the router

// in your form
public function init()
{
    $router = Zend_Controller_Front::getInstance()->getRouter();
    $url = $router->assemble(
        array(
            'paramterName0' => 'parameterValue0',
            'paramterName1' => 'parameterValue1',
        ),
        'routeName'
    );

    $this->setAction($url);
    ...
}

Method 2: Get an instance of the View object and call the url-view-helper directly

// in your form    
public function init()
{
    $url = Zend_Layout::getMvcInstance()->getView()->url(array(), 'routeName';
    $this->setAction($url);
    ...
}

I prefer Method 1. It is more verbose but you have one dependency less in your form.

Joline answered 20/7, 2010 at 18:21 Comment(4)
The problem with method one is that I already have the routes set up in the bootstrap. Method two is fantastic!Alcaraz
@Dickie Method 1 does not define a new Route. It assembles a Url with a existing Route. Take a look in Zend_View_Helper_Url. It does exactly the same: Get the Router and call assemble.Joline
You are right. I mis-read the code. I thought it was creating a new route, not using a route. That is exactly what I am after. Thanks.Alcaraz
I'm not sure why, but I'd not thought of doing this till now. Ben, thanks for a nice and simple solution. Now my forms can stay in sync with my routing table.Lattice
F
1

if in my controller action:

$this->view->form = $form;

I will use view helper url to generate the form action url in my view script ( xxx.phtml):

$url = $this->url(array('controller'=>'my-controller-name', 
                    'action'=>'my-action-name'), 
              'my-route-name'
             );

$this->form->setAction($url);

echo $this->form;
Faxan answered 3/6, 2011 at 5:21 Comment(0)
A
1

Nowadays you can access the Zend_View object via getView() method on Zend_Form classes:

// init your form    
public function init()
{
    $view = $this->getView();
    $url = $view->url(array('module'=>'login','action'=>'login'));
    $this->setAction($url);
    ...
}

Might this help on ZF 1.8+

Avruch answered 25/2, 2012 at 3:53 Comment(0)
A
0

I do not know when it was added, but there is an even simpler solution.

You can retrieve the form's view object with getView(), which has access to the registered routes.

//In the form
$this->setAction($this->getView()->url(array('param1' => 'value1'), 'routeName'));
Alcaraz answered 21/2, 2012 at 15:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.