Skip to content

stickgrinder/fluentform

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FluentForm

This advanced version of Formbuiler was intended as a from-scratch rewrite of original @dperrymorrow's spark, but it quickly evolved in a completely different beast. Its goal is to create a full-fledged form-factory able to define, render and validate a form, providing at the very same time a fluent interface so that form creation is easy to write and read. Full doc will be available soon, in the meantime here is a simple example.

Create a controller named at your wish, and add the following code to it:

$this->load->spark('fluentform/2.0.0');

$form = $this->fluentform->new_form();

$myform->set_action('home')
->add_file_field('image','Upload image')
->add_fieldset('account')
  ->add_text_field('username', 'Username', 'trim|required|minlength[5]|xss_clean')
  ->add_password_field('password')
  ->add_email_field('email')
->add_fieldset('profile', 'Your Profile')
  ->add_text_field('name', 'First Name')
  ->add_checkbox('happy', 'I am happy', 'yes', '', TRUE, array('class'=>'check happy', 'id'=>'mycheck'))
->close_fieldset()
->add_checkboxes('foos', 'Foos list', array(
  array (
    'label' => 'Foo 1',
    'value' => '1',
    'checked' => TRUE,
  ),
  array (
    'label' => 'Foo 2',
    'value' => '2',
    'checked' => TRUE,
  ),
  array (
    'label' => 'Foo 3',
    'value' => '3',
    'checked' => FALSE,
  )))
->add_submit('submit', 'Save')
->add_reset('reset')
->add_button('mybutton', 'Do nothing...');

if (!$form->validate())
{
  echo $form; // magic method to print the form out straight away, or
  //echo $myform->render_form();
}
else
{
  echo "Nice form!";
}

Some nice feature that will soon be documented in further details:

  • Validator extends standard CI one so that you could put your custom validation callbacks in the model. Just add "callback_myfunction_model[mymodel]" into validation rules to invoke $this->mymodel->myfunction() as a validation method.

  • Renderer and validator could be rewritten if necessary: I plan to provide an interface to inherit from withh the stable release. The goal is to allow anybody to implement other renderers (for integration with template engines and such) and maybe more advanced validators. Even if decoupling is not perfect yet, a couple of public functions are already available to inject your custom objects:

    $form = $this->fluentform->new_form();

    $form->set_validator(new Custom_validator($form)); $form_>set_renderer(new Custom_renderer($form));

  • You could print out the whole form just echoing the object or treating it as a string; still you could access following methods:

    echo $form->render_form(); // echoes the form, equals echo $form; echo $form->render_group('profile'); // echoes specified fieldset; echo $form->render_field('name'); // echoes specified field, wrapped by chosen wrapper tags (specified in config file) echo $form->render_raw_field('name'); //echoes specified field, without wrapper but full with label and error

  • In config file you could chose label positions for checkboxed and radiobuttons (before/after/none), validation error method (general/field-by-field) and error messages positions (before/after form open; before/after form close OR before field label/between label and field/after field). You could also configure error wrapper tags and classes.

Questions and Answers

Q: This doc is crap! When REAL docs will be available?
A: Complete CI-Styled docs will be released together with upcoming version 2.1, that will feature some more fluent-magic and features.

Q: How does FB compare to FormGen (issue #4)? Benefits of both?
A: A complete comparison with other solutions will hopefully be available in the upcoming docs. In the meantime, take a look here: stickgrinder/Formbuilder#5

Q: Where could I find FF 1.0?
A: Nowhere. FF was formerly named Formbuilder 2.0 since it was intended as a new version of David Morrow's Frombuilder. I decided to keep numbering from 2 on for the following reasons:

  • I always do a lot of noise about what I do, so someone was expecting a 2.0 version of Formbuilder. I don't want to confuse anyone.
  • David Morrow made me realize that I was fed up with CI's form helpers, bloated controllers and so on. I wanted FB's legacy to stand out.
  • My evil plot is for you to ask, so that I could draw attention to me and feel renowned! :D

Contacts

Credits

Many thanks to @dperrymorrow for inspiring me with his original Formbuilder! :) Kudos!
Many thanks to @philsturgeon for writing DBAD public licence.

License

FluentForm is released under the "Don't Be a Dick Public License", which you can read in all its glory here: http://philsturgeon.co.uk/code/dbad-license

About

A powerful, full featured, fluent form factory for CodeIgniter2 framework

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages