GoodForm DMZ Extensions
GoodForm comes with its own Datamapper DMZ edition. This makes drawing a form for any Datamapper model as easy as $object->form()... and handling a POST request from the form with $object->post_form()!
form (object &goodform [, array fields])
- goodform: pass datamapper a reference to a goodform instance, to add the form elements. | required
- fields: if provided, only these fields will be added to the form. If empty or not provided, all fields in the models validation array will be included. | optional
Example:
$gf = new Goodform();
$u = new User();
$u->get_by_id($user_id);
$u->form($gf, array('name', 'email', 'password'));
$gf->submit('do_login', 'Login');
$form = $gf->generate('users/login');
post_form ([array fields])
- fields: if provided, only post input from these fields will update the model. If empty or not provided, all fields in the models validation array will be update if they have a post value. | optional
Example:
$gf = new Goodform();
$u = new User();
$u->get_by_id($user_id);
if (isset($this->input->post('do_update')))
{
$u->post_form();
if($u->save())
// handle success
else
// handle error
}
Make sure you call form() after post_form(), save() and anything that could change a field value.
Any validation errors will be automatically added next to the relevant form field.
options ([boolean include_null])
This method returns an option array for the model. Used to create dropdown fields for a set of records.
- include_null: if TRUE the first item of the options array will be an empty option. | default: TRUE
Example:
$gf = new Goodform();
$u = new User();
// get subset of user records
$u->get_by_group('admin');
// field spec
$spec = array(
'name' => 'admin_user',
'label' => 'Select Administrator',
'options' => $u->options()
);
// add dropdown to form
$gf->dropdown($spec);
The options array will use the records id as the value and call the models __toString() method as the options string.