Reputation: 109
Just wondering if there are differences between zend forms and normal html forms?
I came to know that we could simply add HTML forms in the view file and it works, however isValid function of form elements wont work for normal HTML forms. Are there any other differences?
I'm building a Zend Framework application for the first time and its quite hard to play with css in zend forms so I was wondering if I could use normal forms while using the features of zend validation like isValid?
Upvotes: 2
Views: 904
Reputation: 7485
If you want to, you can mix them up. You can add validation rules to elements, and elements to forms and validate against the form or each form element. But you don't need to render the form (with the exception of a form with Zend_Form_Element_Hash or like.)
<?php
$form = new Zend_Form;
$form->setMethod('GET');
$e1 = new Zend_Form_Element('first');
$e1->setRequired(true);
$e2 = new Zend_Form_Element('second');
$e2->setRequired(true);
$submit = new Zend_Form_Element_Submit('submit', array(
'label' => 'Do something'
));
$form->addElements(array($e1, $e2, $submit));
if(isset($_GET) && count($_GET)) {
if ($form->isValid($_GET)) {
echo 'Valid';
} else {
echo 'Invalid';
}
}
?>
<form method="get" action="">
First:<input type="text" name="first" id="first" value="">
Second:<input type="text" name="second" id="second" value="">
<input type="submit" name="submit" id="submit" value="Go for it">
</form>
Upvotes: 1
Reputation: 18430
From the Zend Form manual
Zend_Form simplifies form creation and handling in your web application. It performs the following tasks:
Element input filtering and validation
Element ordering
Element and Form rendering, including escaping
Element and form grouping
Element and form-level configuration
It is the third item Element and Form rendering, including escaping
that gives most people trouble because of the markup generated, so they abandon Zend Form, but also have to give up the convenience of the others.
However, you can have the best of both worlds by using the viewScript decorator. My answer here gives an example of how to do this, so I won't repeat myself here.
Basically, Zend Form encapsulates form creation into a class to allow easy manipulation/validation. The form is provided with properties and methods that will make creating and manipulating forms easier.
HTML forms are just markup that has no functionality attached unless you write it yourself. Zend Form will produce that markup for you, or you can produce it yourself as demonstrated in the linked answer.
For further information you may want to read this article about Decorators with Zend_Form by Mathew Weier O'Phinney, the lead developer of Zend Framework.
Upvotes: 3