I am getting very confused here

So many examples, what are we after really.
Examples shown seem really dependent on other stuff, building models, code generation and kind of all other unrelated stuff I think.
Will peraphrase the arborint's ZForm example with addressing his issues on it.
arborint wrote:
1. I needed to pass it a Zend_View to make it work. It seems like it is wired into their View system.
2. The dl/dt/dd tags may seem a little strange. Most people would use div or label I think.
3. We might want to set the class property for form fields and tags containing them. And the dl has a class, but no id.
1. Of course there is some degree of using the already built in components in ZF (Zend_View in this case) but I don't see view as drawback or something, you build a string out of it...it's up to you where you echo it...that doesn't change form handling I think. Seems kind of weird
to have form handled 50/50 by ZForm and some other solution for example.
2. In code below 'no_decorator' element has disabledDefaultDecorators..hence no dl.dt etc... only decorator for rendering the actual element is used.
Clearly decorator design pattern is used here..which allows building any output you want...or none..if you want to use just the meta data of a form element.
Main reason behind setting such default dl dd dt tags for all input elements, is:
* you get pretty good overview of the form by default..with no css
* you get strict html complient code
* in general should be able to decorate those with css anyway you want
* you can very easily disable default decorators if not like it
3. I put
* class for each element
* id for the dl tag in front (first calls in My_Form::init())
* id for dd tag of one specific element
I would do the form something like that as would be possible to use same form in different places. Last element used...has no decorators, hence no html rendered around it
Code: Select all
//index.php
<?php
$my_include = '/path/to/zendframework/library/';
set_include_path($my_include . PATH_SEPARATOR . get_include_path());
include_once 'Zend/View.php';
require_once 'MyForm.php';
$form = new My_Form();
$form->setAction('')
->setMethod('post');
$form->addElement('submit','commit' ,array('label' => 'Send'));
if (!empty($_POST)) {
if ($form->isValid($_POST)) {
echo '<pre>' . print_r($form->getValues(), 1) . '</pre>';
}
}
echo $form->render(new Zend_View());
Code: Select all
//MyForm.php in same dir as index.php
<?php
include_once 'Zend/Form.php';
class My_Form extends Zend_Form
{
public function init()
{
$this->addDecorator('FormElements')
->addDecorator('HtmlTag', array('tag' => 'dl', 'id' => 'dl_id' , 'class' => 'zend_form'))
->addDecorator('Form');
$this->addElement('text', 'fname', array(
'validators' => array(
array('NotEmpty', true, array(
'messages' => array('isEmpty' => 'First name required.')
)),
array('Alnum',false,true),
array('StringLength', false, array(3)),
),
'required' => true,
'class' => 'fname_class',
'label' => 'First name',
'description' => 'Please fill in first name',
));
$this->addElement('text', 'lname', array(
'validators' => array(
array('NotEmpty', true, array(
'messages' => array('isEmpty' => 'Last name required.')
)),
array('Alnum',false,true),
array('StringLength', false, array(3)),
),
'required' => true,
'class' => 'lname_class',
'label' => 'Last name',
'description' => 'Please fill in last name',
));
$this->addElement('text', 'email', array(
'validators' => array(
array('NotEmpty', true, array(
'messages' => array('isEmpty' => 'Email required.')
)),
array('EmailAddress'),
),
'required' => true,
'class' => 'email_class',
'label' => 'Email address',
'description' => 'Give your email address for spamming purposes, please',
));
//->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form'))
$dlDecorator = $this->email->getDecorator('HtmlTag');
$dlDecorator->setOption('id','id_of_dd');
$this->addElement('text', 'no_decorator', array(
'validators' => array(
array('NotEmpty', true, array(
'messages' => array('isEmpty' => 'Email required.')
)),
array('EmailAddress'),
),
'required' => true,
'class' => 'no_decorator_class',
'disableLoadDefaultDecorators' => true,
'decorators' => array(
array('ViewHelper')
)
));
}
}
Code: Select all
<form enctype="application/x-www-form-urlencoded" action="" method="post"><dl id="dl_id" class="zend_form">
<dt><label for="fname" class="fname_class required">First name</label></dt>
<dd>
<input type="text" name="fname" id="fname" value="" class="fname_class"></dd>
<dt><label for="lname" class="lname_class required">Last name</label></dt>
<dd>
<input type="text" name="lname" id="lname" value="" class="lname_class"></dd>
<dt><label for="email" class="email_class required">Email address</label></dt>
<dd id="id_of_dd">
<input type="text" name="email" id="email" value="" class="email_class"></dd>
<input type="text" name="no_decorator" id="no_decorator" value="" class="no_decorator_class">
<dt> </dt><dd>
<input type="submit" name="commit" id="commit" value="Send"></dd></dl></form>