Getting Started
This section will outline common usage of GoodForm. Please refere to the methods section for further more detailed examples.
A Simple Example
Most of the time you will be calling the GoodForm Object from within you controller. Below is a simple controller that uses GoodForm to produce a contact form.
The Controller
The Contact Form is contains four form elements and a submit button. Each field is added to the form one at a time in the order you wish them to appear. All the methods take two parameters or an associative array. With the associative array you can define field attributes for them element, e.g. class, id, size. There are a few special attributes that are used to create extra form items. These are:
- label - The fields label
- description - A brief description of the field
- error - Any validation errors or warning you wish to give the user
These elements will not be added to the field tag but will instead create seperate form elements, any others however will, so make sure you name your attributes correctly else your form will fail to validate with the W3C standards.
// load Goodform library
$this->load->library('goodform');
$this->goodform
// add a label for the name fields
->label('Name', 'first_name')
// add text input
->text('first_name', 'Joe')
// custom HTML
->html(' - ')
// add text input
->text('last_name', 'Wardlaw')
// clear floats hack
->clear()
// clear add a tooltip
->tooltip('Enter your name...')
// add input element with label and tooltip
->text(array('name' => 'email',
'value' => 'jim@gmail.com',
'label' => 'Email',
'size' => 40,
'description' => 'please enter a valid email address'))
// clear floats hack
->clear()
// add textarea for message
->textarea(array('name' => 'message',
'cols' => 40,
'rows' => 10))
// clear floats hack
->clear()
// add checkbox input
->checkbox(array('name' => 'spam_me',
'value' => 1,
'checked' => 'checked',
'label' => 'Spam?'))
// clear floats hack
->clear()
->submit('submit', 'Send');
// construct the form passing the action url
$form = $this->goodform->generate('myform/submit');
The HTML Output
<form action="http://localhost/stwt/www/myform/submit" method="post">
<label for="first_name">Name</label>
<input name="first_name" value="Joe" type="text"/>
<span> - </span>
<input name="last_name" value="Wardlaw" type="text"/>
<div class="clear"></div>
<p class="tooltip">Enter your name...</p>
<label for="email">Email</label>
<input name="email" value="jim@gmail.com" size="40" type="text"/>
<p class="email-tooltip tooltip">please enter a valid email address</p>
<div class="clear"></div>
<textarea name="message" cols="40" rows="10"></textarea>
<div class="clear"></div>
<label for="spam_me">Spam?</label><input name="spam_me" value="1" checked="checked" type="checkbox"/>
<div class="clear"></div>
<input name="submit" value="Send" type="submit"/>
</form>
There you have it. The form variable now contains HTML to render the contact form.