views:

31

answers:

3

All I want to do is make a form, in Zend Framework, that looks like this:

<p id="foo">
<form action="#" method="post">
<label>Start:</label> 
<input id="start" type="text" name="start" /> 
<label>End:</label> 
<input id="end" type="text" name="end" /> 
<input type="submit" name="submit" value="Submit" />
</form>
</p>

and I simply cannot do it. :( I can stick in in the view like that but then I don't get validation and filtering.

Any tips on how to figure out decorators and Zend_Form?

A: 

Here is an example of how I create a Zend form:

class Mobile_Form_Login extends Zend_Form
{
    public function init()
    {
        $this->setName('authenticate');

        $nick = $this->createElement('text', 'nick');
        $nick->setRequired(true);
        $nick->addValidators(array(
            new Zend_Validate_StringLength(3, 10),
        ));

        $pass = $this->createElement('password', 'password');
        $pass->setAttrib('size', 10);
        $pass->setRequired(true);
        $pass->addValidators(array(
            new Zend_Validate_StringLength(6, 128),
        ));

        $this->addElements(array($nick, $pass));
    }
}

The later, in my controller

$form = new Mobile_Form_Login();
if (!$form->isValid($_POST)) {
     // handle errors
}

// else render()
thomasmalt
Yes but without decorators you cannot generate the code I used in my question. Decorators are the whole problem, the rest of Zend Form I get.
rg88
+1  A: 

If you are comfortable working with views but also needs the validation and filter features, you can still use Zend Form.

class Default_Form_Search extends Zend_Form
{
  public function init()
  {
    $start = new Zend_Form_Element_Text('start');
    $start->setRequired(true)
      ->addValidator('Date', false, array('format' => 'YYYY-MM-dd'))
      ->setDecorators(array(
        array('ViewHelper')
      ));

    $this->addElement($start);

    // For end
  }
}

In your controller:

$form = new Default_Form_Search;
$form->setAction('/search');

if ($this->getRequest()->isPost())
{
  if ($form->isValid($this->getRequest()->getPost())
  {
    // Search here
  }
  {
    var_dump($form->getMessages();
  }
}

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

Then in your view, you simply form the HTML as is but output the element like this:

<p id="foo">
<form action="<?php echo $this->form->getAction() ?>" method="post">
<label>Start:</label> 
<?php echo $this->form->start ?>
<label>End:</label> 
<?php echo $this->form->end ?>
<input type="submit" name="submit" value="Submit" />
</form>
</p>
Lysender
This is a very useful suggestion. I still want to "get" decorators but this will solve my problem. Thx, Lysender.
rg88
You can play with element decorators. Try using 'Label' + 'ViewHelper' only for elements. Like: $element->setDecorators(array(array('Label'), array('ViewHelper')));
Lysender
+1  A: 

This works. I've adapted this from a form I'm using so I stuck in a few other things like ID's that you can use if you like. Useful to see how it all comes together anyway:

In my controller:

private function _theForm() {
$form = new Zend_Form;
        $form->setAction('/controller/action')
                ->setMethod('post')
                ->addAttribs(array('id' => 'an_id_for_the_form'));

        $form->addElement('text', 'start', array(
            'validators' => array(
                'NotEmpty'
            ),
            'label' => 'Start:',
            'id' => 'someid',
            'required' => true,
            'decorators' => array(
                'ViewHelper',
                'Errors',
                'Label',
            ),
        ));
        $form->addElement('text', 'end', array(
            'validators' => array(
                'NotEmpty'
            ),
            'label' => 'End:',
            'id' => 'someotherid_if_you_like',
            'required' => true,
            'decorators' => array(
                'ViewHelper',
                'Errors',
                'Label',
                ),
        ));
        $form->addElement('submit', 'submitSomething', array(
            'label' => 'Submit',
            'decorators' => array(
                'ViewHelper',
            )
            ));

        $form->setDecorators(array(
            'FormElements',
            array('HtmlTag', array('tag' => 'p', 'id' => 'foo')),
            'Form',
        ));

        return $form;
}

I then call that elsewhere in my controller, like:

$form = $this->_theForm();
$this->view->form = $form;

Then you can use it in your view like:

 <?php echo $this->form; ?>
gaoshan88