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.
free viagra
buy viagra online
generic viagra
how does viagra work
cheap viagra
buy viagra
buy viagra online inurl
viagra 6 free samples
viagra online
viagra for women
viagra side effects
female viagra
natural viagra
online viagra
cheapest viagra prices
herbal viagra
alternative to viagra
buy generic viagra
purchase viagra online
free viagra without prescription
viagra attorneys
free viagra samples before buying
buy generic viagra cheap
viagra uk
generic viagra online
try viagra for free
generic viagra from india
fda approves viagra
free viagra sample
what is better viagra or levitra
discount generic viagra online
viagra cialis levitra
viagra dosage
viagra cheap
viagra on line
best price for viagra
free sample pack of viagra
viagra generic
viagra without prescription
discount viagra
gay viagra
mail order viagra
viagra inurl
generic viagra online paypal
generic viagra overnight
generic viagra online pharmacy
generic viagra uk
buy cheap viagra online uk
suppliers of viagra
how long does viagra last
viagra sex
generic viagra soft tabs
generic viagra 100mg
buy viagra onli
generic viagra online without prescription
viagra energy drink
cheapest uk supplier viagra
viagra cialis
generic viagra safe
viagra professional
viagra sales
viagra free trial pack
viagra lawyers
over the counter viagra
best price for generic viagra
viagra jokes
buying viagra
viagra samples
viagra sample
cialis
generic cialis
cheapest cialis
buy cialis online
buying generic cialis
cialis for order
what are the side effects of cialis
buy generic cialis
what is the generic name for cialis
cheap cialis
cialis online
buy cialis
cialis side effects
how long does cialis last
cialis forum
cialis lawyer ohio
cialis attorneys
cialis attorney columbus
cialis injury lawyer ohio
cialis injury attorney ohio
cialis injury lawyer columbus
prices cialis
cialis lawyers
viagra cialis levitra
cialis lawyer columbus
online generic cialis
daily cialis
cialis injury attorney columbus
cialis attorney ohio
cialis cost
cialis professional
cialis super active
how does cialis work
what does cialis look like
cialis drug
viagra cialis
cialis to buy new zealand
cialis without prescription
free cialis
cialis soft tabs
discount cialis
cialis generic
generic cialis from india
cheap cialis sale online
cialis daily
cialis reviews
cialis generico
how can i take cialis
cheap cialis si
cialis vs viagra
levitra
generic levitra
levitra attorneys
what is better viagra or levitra
viagra cialis levitra
levitra side effects
buy levitra
levitra online
levitra dangers
how does levitra work
levitra lawyers
what is the difference between levitra and viagra
levitra versus viagra
which works better viagra or levitra
buy levitra and overnight shipping
levitra vs viagra
canidan pharmacies levitra
how long does levitra last
viagra cialis levitra
levitra acheter
comprare levitra
levitra ohne rezept
levitra 20mg
levitra senza ricetta
cheapest generic levitra
levitra compra
cheap levitra
levitra overnight
levitra generika
levitra kaufen
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!