The Form tutorial - Formation
- Posted by dlib on March 21st, 2008 filed in Form tutorial, Formation, Tutorials
After you’ve set up and the model as seen in a previous post we can now begin with building a form using Formation.
First we need to change the model a little:
models/preference.php
class Preference_Model extends ORM { protected $validate=array( 'name'=>array( 'rules'=>array(array('Rule_Max_Length',50)), 'pre_filters'=>array('ucfirst') ), 'email'=>array( 'type'=>'email', 'rules'=>array('Rule_Required'), ), 'preference'=>array( 'type'=>'checklist', ), 'comment'=> array( 'pre_filters'=>array('trim')) ); }
You see that where in the normal validation library I set the rules in the controller, I now set them in the model. You see I also set the type of form field for some elements, the type defaults to input but it is derived from the comment database field that it is an textarea.
Now we need to declare the model
libraries/Preference_Form.php
class Preference_Form_Core extends Model_Formation{ protected $disabled=array(); protected $form_fields=array(); protected $exclude=array('id'); protected $_model='Preference_Model'; public function __construct() { parent::__construct(); $foods = array ( 'spaghetti' => array('spaghetti', FALSE), 'pizza' => array('pizza', FALSE), 'french fries' => array('french fries', FALSE), 'sauerkraut' => array('sauerkraut', FALSE), ); $this['preference']->set_options($foods); } }
In the properties I set that no fields are disabled, no fields are explicitly shown but the id field is explicitly not shown. The model this form works on is the Preference_Model. Furthermore I set the options in the checkboxes.
The Model_Validation class also adds extra rules to the fields when the length of a field is known in the database. E.g. varchar(255) gets a rule Max_Length with 255
Now for the controller:
public function insert_formation() { $form=new Preference_Form; if($form->save(false)) { $form->model()->preference=serialize($form->model()->preference); $form->model()->save(); } else { echo $form; } }
I save the form but set commit to false so it isn’t actually saved, it’s just validated. I serialize the preference array and then save it. The form will output error messages if any. The form is also populated on submit.
To use fieldsets in a group:
public function __construct() { parent::__construct(); $this->add_group(array('preference'),'Preferences'); $foods = array ( 'spaghetti' => array('spaghetti', FALSE), 'pizza' => array('pizza', FALSE), 'french fries' => array('french fries', FALSE), 'sauerkraut' => array('sauerkraut', FALSE), ); $this['Preferences']['preference']->set_options($foods); $this->asort(); }
I also sort the form although it isn’t really useful here it should be clear that you can use sort algorithms as well as count and other array related functions to handle the form object.
Since you declared the rules in the model it should also be easy to have a validate method in the model so as to validate before saving anything.
You can find Formation on google code the latest version is always available from the google code svn. Just drop the module in your modules folder and activate it.
March 23rd, 2008 at 9:15 pm
Your ModelToForm doesn’t work for me. $this->_model->list_fields() works fine but build_form() method generates a form without any fields (only submit button is generated). I guess there is a problem with following foreach:
March 23rd, 2008 at 10:44 pm
I have no problems. Did you do the MY_ORM.php extension properly? It contains the list_fields() method so when the file is not there it will break. Also, you can have only one MY_ORM file in your application and modules folders as Kohana will load only one.
March 23rd, 2008 at 11:09 pm
Yes it was caused by My_ORM class. Thanks.
April 15th, 2008 at 1:53 pm
how ORM helps when we have an array of table cols as key and its values. How can we assign that array to save()
April 15th, 2008 at 2:10 pm
I have a method load_values in my MY_ORM.php file. I think it’s in the Formation package but there is also a post on the subject.
April 16th, 2008 at 2:37 pm
thanks dlib. I will have a look over it.
August 13th, 2008 at 5:11 pm
FYI,dlib, the top link in this article gives a 404:
http://h1368840.stratoserver.net/2008/03/21/the-form-tutorial-the-beginning/
August 13th, 2008 at 5:41 pm
Hi dlib, is there any other place (different to google code) from where to get Formation, Forge and Media modules?
Previous to 2.2 verion, Forge and Media were available on the Kohana distribution, Formation never was.
I live in Cuba and Google does not allow us to download software nor packages from any of their servers.
Our team have started to develop web sites using Kohana and now we can’t use nothing else but the modules avaible in the distribution ZIP archive.
Thanks in advance.
August 13th, 2008 at 9:43 pm
I’ll place a copy of Formation on this server (Europe based) shortly for you. You can still get Forge and the Media module from the svn repository, just use a revision just before the 2.2, there’ve been no updates since.
Expect Formation to be there Friday, don’t have time till then. It’ll be the new 2.2 release probably.
Rastatech: 404 fixed, we moved domains a while ago.
August 14th, 2008 at 2:14 pm
Thanks dlib, I look forward to the 2.2 formation!