views:

257

answers:

2

Hello! I want to use Zend_Form because of its validation and filters etc. But the markup for forms is already done. Even worse, it isn't very standardized, so writing my custom decorators is not a solution.

Is there a simple way to "apply" Zend_Form to existing very custom markup? I think it's a common problem.

+9  A: 

If you use the same names in your hand-made-HTML-form as in your Zend_Form you can simply instantiate the form on data reception:

public function processFormAction()
{
    $form = new My_Form(); // this is your Zend_Form
    if ($form->isValid($_POST)) {
        // success!
    } else {
        // failure!
    }
}

There is no need to use the rendering capabilities of Zend_Form if you don't want to.

Second option is to combine your custom markup with the form's elements:

// view-script with $form being the the Zend_Form passed into the view
<form method="<?php echo $form->getMethod() ?>" action="<?php echo $form->getAction()?>">
    <div id="elements">
        <?php echo $form->element1->renderLabel() . $form->element1->renderViewHelper() ?>
        <br />
        <?php echo $form->element2->renderLabel() . $form->element2->renderViewHelper() ?>
    </div>
    <div id="buttons">
        <?php echo $form->button1->renderViewHelper() ?>
        &nbsp;
        <?php echo $form->button2->renderViewHelper() ?>
    </div>
</form>

Alternatively, if you only want to filter and validate your input, you can avoid all the bloat of Zend_Form and use Zend_Filter_Input, which is, simply spoken, Zend_Form without all the markup stuff. As Zend_Filter_Input can use all the standard and custom filters and validators you're able to use with Zend_Form, the transition should be fairly easy.

Stefan Gehrig
All I really want to get from render is validation errors and form values :)
valya
+1  A: 

There's a much neater way of doing this, with the ViewScript form decorator, specifically designed to render forms into custom markup. It works in a similar way to the example given by Stefan but you can use your own scripts or partials, and you need to do much less work if all you want to do is put elements, their labels and errors into custom markup.

See the Official Reference Guide and more specifically this Zend devzone article.

David Caunt