Autoform Spark for Codeigniter
Autoform is a Library that takes the hassle out of creating html forms. It can generate entire forms based on a table in your database, with all validation code in place.
Note: Autoform Loads the array and url Helpers and form_validation class(if they have not already been loaded)
Features:
- Quickly Build entire form, including labels & validation error messages for each element
- Forms can be automatically generated from a database table or sql query
Labels & Error Messages
Labels are added in before each field, the contents of the label(by default - it can be overwritten) is the name of the field capitalized and underscores set to spaces. eg: a field named "my_field" would have a label "My Field".
Errors, by default will replace the label content. This can be stopped by setting $this->autoform->inline_errors = FALSE;.
Fields that have an error message will have the classname "error" added to both the label and the field itself.
Database generation
This is an example of how to build a form automatically from a database.
Generate form from a database table
Specify the database table name
class Form_example extends CI_Controller {
function index() {
$this->load->spark('autoform/[version]');
$this->autoform->table('my_table');
echo $this->autoform->generate('form_example/test');
}
function test() {
$this->load->spark('autoform/[version]');
$this->form_validation->set_rules('name','Name','trim|required');
$this->form_validation->set_rules('email','Email','trim|required|valid_email');
if ($this->form_validation->run() == FALSE) {
$this->autoform->table('my_table');
echo $this->autoform->generate('form_example/test');
}
else {
// do processing
}
}
}
Generate form from a database query
Sometimes, its usful to to use a query result to generate your form - with all values pre-populated.
Do this by either entering an SQL query or even a CI query object into the sql() method.
$this->autoform->sql('SELECT id, name, email FROM my_table WHERE id = 1');
echo $this->autoform->generate('form_example/test');
// or:
$query = $this->db->get_where('my_table', array('id'=>1));
$this->autoform->sql($query);
echo $this->autoform->generate('form_exmaple/test');
Customize output
You probably don't always want to output every field in your database table. so here are a few examples of how to customize the final form.
function index() {
$this->load->spark('autoform/[version]');
$this->autoform->table('my_table');
// remove some fields
$this->autoform->remove(array('unwanted_field','another_unwanted_field'));
// change some attributes of a field
$this->autoform->set('email', array('type'=>'email', 'class'=>'classname', 'required'=>'required'));
$this->autoform->set('name', array('value'=>$name)); // note: the value with always be overwritten on validation to the posted value.
echo $this->autoform->generate('form_exmaple/test');
}
Output Separation
You can even output each part of the form separately. Note: the open / close methods are the same as Ci's form helper with the exception of the third parameter for open(), which is a boolean that sets the form as multipart.
function index() {
$this->load->spark('autoform/[version]');
$this->autoform->table('my_table');
echo $this->autoform->open('form_exmaple/test', array('id'=>'form'), FALSE);
echo $this->autoform->fields(array('email', 'name')); // if this parameter is left empty, all fields are returned in the order they were created.
echo $this->autoform->field('id');
echo $this->autoform->buttons;
echo $this->autoform->close();
}
Adding fields manually
A lot of the time you will need to add extra fields, or not even use a database at all. Here's how:
Notice how how the add() method also returns the compiled field.
Here I've included an example of changing the labels text & attributes
function index() {
$this->load->spark('autoform/[version]');
echo $this->autoform->open('form_exmaple/test', array('id'=>'form'), TRUE); // third param set the form as multipart, for uploading files
echo $this->autoform->add(array('name'=>'upload', 'type'=>'file', 'label'=>'Upload your file here'));
echo $this->autoform->add(array('name'=>'tick_me', 'type'=>'checkbox', 'value'=>1, 'checked'=>FALSE, 'label'=>array('content'=>'Tick me before you can upload','class'=>'monkey')));
echo $this->autoform->add(array('name'=>'choose_me', 'type'=>'select', 'value'=>1, 'options'=>array('1'=>'option 1', '2'=>'option 2')));
echo $this->autoform->buttons;
echo $this->autoform->close();
}
Autoform Function Reference
$this->autoform->add()
Adds a field to the form. Expects an array of attributes. returns the compiled label/field.
The name attribute is required, and if you do not supply them, both id and the label use the name.
Note: Don't echo this if you are using the Generate() method, or you'll get it twice.
echo $this->autoform->add(array('name'=>'foo','type'='text', 'value'=>'monkeys'))
$this->autoform->remove()
Remove unwanted fields from the form
$this->autoform->remove(array('field1_id','field2_id'))
$this->autoform->set()
Set/Change attributes of a field
$this->autoform->set('field_id', array('attr'=>'value'))
$this->autoform->before()
Insert a string before a field(in front of the label)
$this->autoform->before('field_id', '<div class="wrapper">')
$this->autoform->after()
Insert a string after a field
$this->autoform->after('field_id', '</div>')
$this->autoform->wrap_each()
Insert a string before & after all fields (or a subset of fields). Leaving the third parameter empty will wrap all fields
$this->autoform->wrap_each('</p>', '</p>', array('field1','field3'))
$this->autoform->field()
Get a field's finalized html
$this->autoform->field('field_id')
$this->autoform->fields()
Returns the finalized html string of all fields in the form.
The optional first paramter is an array of field id's of which to output(in the order specified in the array)
$this->autoform->fields(array('field3','field1','field2'))
$this->autoform->get()
Returns a single value or an array of values for the supplied attributes
$this->autoform->get('field_id', array('value','class'))
$this->autoform->get('field_id', 'value')
$this->autoform->label()
Returns the label of a form. Third parameter is a boolean, if set to FALSE only the label's text will be returned without the label tag.(errors will be wrapped in <span class="error">)
$this->autoform->label('field_id','Fallback text(used instead of default label)', FALSE)
$this->autoform->open()
Returns the opening tag of the form. The first parameter is the uri to submit the form to.
The second is an array of attributes, and the third is a boolean(TRUE/FALSE) of whether the form is encoded as multipart, for file uploads.
$this->autoform->open('form/process', array('id'=>'my_form'), TRUE)
$this->autoform->close()
Returns the close tag of the form(exactly the same as CI's form_close() function).
$this->autoform->close('optional text to append to the returned string')
$this->autoform->generate()
Returns the entire form. parameters are the same as $this->autoform->open()
$this->autoform->generate('form/process', array('id'=>'my_form'))
$this->autoform->table()
Specifies a database table to base the form on
$this->autoform->table('table_name')
$this->autoform->sql()
SQl query string(or CI query result object) to base the form on
$this->autoform->sql('SELECT * FROM table_name')
or use a query object
$query = $this->db->get('my_table');
$this->autoform->sql($query)
$this->autoform->clear()
Resets the form(removes all fields and buttons)
$this->autoform->clear()
$this->autoform->buttons()
Sets the forms submit buttons.
$this->autoform->buttons(form_submit(array('type'=>'submit', 'content'=>'submit form')))