Model to Form generation
In my previous post I gave an explanation on how you can save a Formation or Forge form as a library. This goes into that a little further. It’s inspired by similar functionality in Django.
I’ve made an extension to the Formation library, it’s not public yet but I feel like sharing how it works. It also involves some extra methods for the ORM library which are useful outside of this library as well. What it does is simple but best explained by some code.
$form=new User_Form(new User_Model(5)); //retrieves model with id=5 if($form->save()) { echo 'user is saved into database'; //it uses the model passed } else { echo $form->render(); //outputs the entire form with values from user table id =5 }
This might be a little weird at first sight. What it does is generate a form from your ORM model. This is really easy if you want a form that maps to your model.
So what’s happening. The User_Form extends Model_Formation which extends Formation. What’s extra is that the Model_Formation class accepts an ORM model as an argument (normal models are possible but you’d have to do some coding). This can be a empty model and you’ll get a empty form, or a filled model which will give you a filled form.
If you want your models to pass some rules, filters or callbacks this is possible. You have to declare them in your model and the Model_Formation class will retrieve them and build a form given those rules. Using the jQuery extension you can even have javascript validation in your view with rules retrieved from the model.
In the User_Form.php file you can then declare which fields you want to show and omit. You might want to leave out the id field, or date fields.
Now, if you instantiate the User_Form a form will be created with fields and rules retrieved from the model. Also, varchar(200) fields will receive a rule with max_length=200. I’m even working on boolean fields becoming checkboxes and maybe creating dropdowns when you have relationships. You can always change labels, add fields etc. like you normally could in the Formation class.
When you call the save() method of the class, the model will first try to validate and then it will be saved into the database. If saving fails you can retrieve errors, render the form or I don’t know what.
I find it very useful for backends. There is still some things that need a little work, boolean db fields need a checkbox, ‘text’ fields a textarea. You might want to display items but not allow editing etc. I’ll try to release it shortly and would welcome any feedback.
I’ve added some documentation to the repository.
March 14th, 2008 at 9:58 am
Wow, that’s really clever and very helpful. There are great articles on your website maybe some of them could be part of tutorials in the kohana website. I think I’m going to jump in your Formation library as it turns out to be cooler to use that the current validation and forge library.
Thanks and keep up the good work!
March 14th, 2008 at 10:05 am
For example, all the articles “kohana under the hood” explaining the loading process of kohana are really useful to understand the magic. They could be really valuable in the doc wiki.
Just my 2 cents.
March 14th, 2008 at 11:42 am
I really like the direction this is taking. Could speed up admin interface generation considerably.
March 17th, 2008 at 3:09 pm
I’m not having much luck using Model_Formation as you can se from my comments :
http://code.google.com/p/kohana-mptt/wiki/ModelToForm
Maybe I’m not getting it or something…