Auto fORM - Auto Modeler + Form Generation for Kohana

I was looking at Argentum Invoice recently, and discovered the plugin Auto Modeler, it’s a great plugin which makes using models in Kohana so much easier. Auto Modeler also has validation support, so I decided to integrate Phorm into it. The result is Auto fORM. I changed the name from Phorm to Auto fORM because 1) I like the play on letters (ORM) 2) the name Phorm is already being used for another php generation tool 3) to give homage to Auto Modeler.

Features:

  • New name
  • Uses Auto Modeler’s validation
  • Reduces coding time
  • Fills in forms automagically
  • Improved autoloading function which now detects controller folder depth.
  • Specify rules within Model

Here’s a quick comparision and contrast of some sample code from Argentum Invoice (0.5).

Download
Github

Model: models/client.php

Class function have been removed. TO use Auto fORM make sure you put extends Auto_fORM. You must declare rules here.


class Client_Model extends Auto_fORM
{
	protected $table_name = 'clients';

	protected $data = array('id' => '',
	                        'company_name' => '',
	                        'short_name' => '',
	                        'contact_first_name' => '',
	                        'contact_last_name' => '',
	                        'mailing_address' => '',
	                        'mailing_city' => '',
	                        'mailing_state' => '',
	                        'mailing_zip_code' => '',
	                        'email_address' => '',
	                        'phone_number' => '',
	                        'tax_rate' => 0.0);

	protected $rules = array('company_name' => array('required'),
	                         'contact_first_name' => array('required', 'standard_text'),
	                         'contact_last_name' => array('required'),
	                         'email_address' => array('required', 'email'),
	                         'phone_number' => array('phone'),
	                         'mailing_zip_code' => array('numeric'),
	                         'tax_rate' => array('numeric'),
	                         'mailing_address' => array('required'),
	                         'mailing_city' => array('required'),
	                         'mailing_state' => array('required'),
	                         'mailing_zip_code' => array('numeric'));
}

Controller: controllers/admin/client.php


public function add()
{
	$client = new Client_Model();
	if ( ! $_POST) // Display the form
	{
		$this->template->body = new View('admin/client/form');
		$this->template->body->errors = '';
		$this->template->body->client = $client;
		$this->template->body->title = 'Add';
	}
	else
	{
		$client->set_fields($this->input->post());

		try
		{
			$client->save();
			url::redirect('client/view/'.$client->short_name);
		}
		catch (Kohana_User_Exception $e)
		{
			$this->template->body = new View('admin/client/form');
			$this->template->body->client = $client;
			$this->template->body->errors = $e;
			$this->template->body->title = 'Add';
		}
	}
}

Into:


public function add()
{
	$client = new Client_Model();
	$client->template('form');
	if ($client->valid())
	{
		$client->save();
		url::redirect('client/view/'.$client->short_name);
	}
	else
	{
		$this->template->body = $client->render();
		$this->template->body->client = $client;
		$this->template->body->title = 'Add';

	}
}

View: views/admin/client/form.html


<h2><?=$title?> Client</h2>
<?=$errors?>
<?=form::open()?>
<ul>
	<li><label for="company_name">Company Name:</label> <?=form::input('company_name', $client->company_name)?></li>
	<li><label for="contact_first_name">Contact First Name:</label> <?=form::input('contact_first_name', $client->contact_first_name)?></li>
	<li><label for="contact_first_name">Contact Last Name:</label> <?=form::input('contact_last_name', $client->contact_last_name)?></li>
	<li><label for="mailing_address">Mailing Address:</label>
<?=form::textarea(array('name' => 'mailing_address', 'value' => $client->mailing_address))?></li>
	<li><label for="mailing_city">Mailing City:</label> <?=form::input('mailing_city', $client->mailing_city)?></li>
	<li><label for="mailing_state">Mailing State:</label> <?=form::input('mailing_state', $client->mailing_state)?></li>
	<li><label for="mailing_zip_code">Mailing Zip Code:</label> <?=form::input('mailing_zip_code', $client->mailing_zip_code)?></li>
	<li><label for="email_address">Email Address:</label> <?=form::input('email_address', $client->email_address)?></li>
	<li><label for="phone_number">Phone Number:</label> <?=form::input('phone_number', $client->phone_number)?></li>
	<li><label for="tax_rate">Tax Rate:</label> <?=form::input('tax_rate', $client->tax_rate)?></li>
	<li><?=form::submit('submit', $title.' Client')?></li>
</ul>
<?form::close()?>

<h2><?=$title?> Client</h2>
<?=aform::errors()?>
<?=aform::open()?>
<?aform::labels_for('input', 'textarea')?>
<ul>
	<li><?=aform::input('company_name')?></li>
	<li><?=aform::input('contact_first_name')?></li>
	<li><?=aform::input('contact_last_name')?></li>
	<li><?=aform::textarea(array('name' => 'mailing_address'))?></li>
	<li><?=aform::input('mailing_city')?></li>
	<li><?=aform::input('mailing_state')?></li>
	<li><?=aform::input('mailing_zip_code')?></li>
	<li><?=aform::input('email_address')?></li>
	<li><?=aform::input('phone_number')?></li>
	<li><?=aform::input('tax_rate')?></li>
	<li><?=aform::submit('submit', $title.' Client')?></li>
</ul>
<?=aform::close()?>

Now let’s go a little more in depth. Here are a list of changes from the previous version.

  • Name change from Phorm to Auto_fORM
  • Cruft and crummy code taken out.
  • All templates must be within a ‘views’ folder.
  • Config options removed
  • Validation class deprecated in favor of Auto Modeler’s validation. Rules are declared in Model Class.
  • Improved auto loading function. Now detects if controller file is nested within a folder.
  • Css support has been deprecated. It’s easier to put css within error template inside style tags.
  • $object->fetch() has been changed to render() and it returns a view object.

Configuration Options:

  • add_form_name: add form name to elements. e.g. myElement name becomes myForm[myElement] and id becomes myForm_myElement. Does not apply to select multiples.
  • controller_sub_folder: use the controller name as a subfolder for views. Does not apply to error views. e.g. Controller Name = Quick_Controller; path = ‘views/quick/myform.php’
  • error_class: error class to add to elements that did not validate.
  • label_error_class: error class to add to labels for elements that did not validate. Can be set to FALSE.
  • errors_as_labels: add label tags to error messages so that they can become clickable.
  • blank_passwords: blank password fields on validation errors

Auto_fORM Library Methods:

  • is_valid(’elementName’): check if element is valid. Pass element name and receive boolean.
  • data: pass data array for rules to be validated against.
  • errors: errors for form. Run through error template. Returns html.
  • lang: lang file to use.
  • name: name of form.
  • template: form template to use.
  • render: render form template. Returns a View object.
  • valid: is form valid? Returns boolean.

Use template ‘wow’, custom lang file ‘myLang’, form name ‘myForm’ and use $_GET for validation.

controllers/admin/client.php


class Client_Controller
{
    function add()
    {
        $demo = new Demo_Model();
        $demo->template('wow')->lang('myLang')->name('myForm')->data($_GET);
    }
}

If using a custom lang file, must use Kohana’s layout specification. Element names will be wrapped with myForm[$name] and $_GET will be used for validation. Okay now here’s fun the part. The default form to be loaded would be ‘add’ because of the function name. However we specify wow to be used. If you notice the controller is within the folder admin, so controllers will look for wow in ‘views/admin/client/wow.php’.  To override this autoloading feature specify the full path.

aform Helper Methods:

All methods from Kohana’s Form Class are supported and compatible. Listed are additional methods provided by aform.

  • errors: return form errors parsed through error template. Error template name can be passed.
  • is_valid: pass element name and returns boolean if element is valid or not
  • error_count: returns number of errors for form
  • multiple: select multiple. same parameters as dropdown.
  • wrap: wrap specific elements with text or html
  • labels_for: elements to wrap with a label. Label text is automatically parsed. Form name is removed, underscores are replaced with spaces and element name is passed through ucwords.

Add lables for input and textarea tags. And wrap input and button elements with html. And display form errors


<?=aform::errors()?>
<?aform::wrap(array('input','textarea'), 'prefix', '<br/>')?>
<?aform::labels_for(array('input','textarea'))?>

To customize the default error template look in ‘auto_form/views/errors/auto_form_errors.php’. Put custom error templates into any errors folder within a views folder. I love Kohana’s cascading file system.

Trackback URL

, ,

One Comment on "Auto fORM - Auto Modeler + Form Generation for Kohana"

Trackbacks

  1. [...] UPDATE: New version of Phorm here. [...]

Hi Stranger, leave a comment:

ALLOWED XHTML TAGS:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to Comments