Skip to content
johanlindblad edited this page Sep 19, 2011 · 5 revisions

How to use

What you need

  • Kohana 3.2 (3.1 may also work. 3.0 probably not)
  • Kostache
  • MangoDB if you want to import Mango models

Let's go

Create a form:

$form = Latter::factory('url/to/submit/to', 'POST');

Add some fields:

$form->field('name', 'text', array('label' => 'Name'));
$form->field('password', 'password', array('label' => 'Password'));
$form->field('birth_date', 'date', array('label' => 'Birth date'));

Choose which buttons you want:

$form->buttons(array(
	'submit' => array('label' => 'Submit'),
	'reset' => array('label' => 'Reset'),
));

Load the the values from either from our own array...:

$form->load(array('name' => 'John Doe', 'password' => '123'));

...or let Latter guess based on the form's method:

$form->load(); // This will load from $_POST

If the form was sent, do stuff with the values:

if($form->sent())
{
	echo Debug::dump($form->values());
}

Render it:

$view = new View_Layout('path/to/your/template');
$view->form = $form->render();
$this->response->body($view);

Put this in your template:

{{& form}}

or

{{#form}}
{{& open}}
{{& fields}}
{{& buttons}}
{{& close}}
{{/form}}

or

{{#form}}
{{& open}}
{{#fields}}
<fieldset>
	<legend>Fields:</legend>
{{& name}}
{{& password}}
{{& birth_date}}
{{/fields}}
<fieldset>
	<legend>Buttons:</legend>
{{& buttons}}
</fieldset>
{{& close}}
{{/form}}

or even

{{#form}}
{{& open}}
{{#buttons}}
<h1>Edit *username*</h1>{{& reset}}
{{#fields}}
{{& name}}
{{& password}}
{{& birth_date}}
{{/fields}}
{{& submit}}
{{/buttons}}
</fieldset>
{{& close}}
{{/form}}

So as you can see, you can be very specific but enjoy great flexibility or just let Latter go on autopilot and render the form in a way that probably works fine in 90% of cases.

Extra: import a MangoDB model

First create the form in the same way as above. Then anywhere before the call to load():

$model = Mango::factory('model_name');
$form->import($model);

If you run validate() on the form, the Mango model will be checked separately and any errors will be added to the form fields:

$form->validate();
if($form->valid())
{
	// Do stuff. You know that $model is valid.
	// Please note that if the form hasn't been submitted, this block will not run.
}