Skip to content

forms creation

Harmen Janssen edited this page Aug 22, 2016 · 3 revisions

Creating a form

The following is an example login form:

$form = new Garp_Form();
$form->setAction('/my/action/')
     ->setMethod('post')
     ->setAttrib('id', 'my-super-awesome-form');
$form->addElement('email', 'email', array(
	'label' => 'Your email address',
	'required' => true
));
$form->addElement('password', 'password', array(
	'label' => 'Your password',
	'required' => true
));
$form->addElement('submit', 'Register');
echo $form;

Done. Usually you probably want to do the echoing in a view and constructing of the form in a controller. The parameters you pass to addElement() are, in order, $type, $name, $options. The former 2 are pretty self-explanatory, the 3rd is an array containing tons of configuration options, some of which will be featured in the examples below. All input types are supported, even the new HTML5 types. One other method of Garp_Form that's interesting is createElement(), which returns a Zend_Form_Element object. Use this if you want to further act upon a certain field:

$username = $form->createElement('text', 'username', array(
	'label' => 'Username'
));
$username->setAttrib('autocomplete', 'off');
$form->addElement($username);

Back to forms

Clone this wiki locally