views:

25

answers:

3

Here is my code:

<?php

class IndexController extends Zend_Controller_Action
{

    public function indexAction()
    {
        $this->_forward('search');
    }

    public function searchAction()
    {
        if($this->getRequest()->isGet())
        {
            $form = $this->getForm();

            $value  = $form->getValue('search');
            echo "'", $value, "'"; // <-- prints ''
        }

        $this->view->form = $this->getForm();
    }

    public function getForm()
    {
        $form = new Zend_Form();
        $form->setAction('/index/search')
             ->setMethod('get');

        $search = $form->createElement('text', 'search');
        $search->addFilter('StripTags')
               ->addFilter('StringTrim');

        // Add elements to form:

        $form->addElement($search)
             ->addElement('submit', 'submit', array('label' => 'Search'));

        return $form;
    }
}

In searchAction() $value is always blank even after I submit the form and I see the data in the URL. What is the problem?

EDIT: I fixed getValue() to getValues() but it still isn't working.

A: 

The function you want is getValue, not getValues.

A subtle difference but they do two totally different things.

David Caunt
OK, I fixed that but still no love. I can't get $value to be anything but empty.
moteutsch
+2  A: 

Before you are able to use ->getValue(); you have to validate the form.

 public function searchAction()
    {
        if($this->getRequest()->isGet())
        {
            $form = $this->getForm();

            if ($form->isValid()) {
                $value  = $form->getValue('search');
                echo "'", $value, "'"; // <-- prints ''
            } else {
                // what ever
            }



        }

        $this->view->form = $this->getForm();
    }
ArneRie
+1  A: 

You need to pass $this->_request->getParams() to $form->isValid(), otherwise the form will not have any values to work with.

Adrian Schneider