The Form tutorial - Forge
- Posted by dlib on March 21st, 2008 filed in Forge, Form tutorial, Tutorials
I have now shown how to use Formation and the new Kohana validation library with forms. I will now demonstrate the same form with Forge. Forge has no filters built-in so you can apply them manually. That should be a breeze so I won’t go into it.
public function insert_forge(){ $foods = array ( 'spaghetti' => array('spaghetti', FALSE), 'pizza' => array('pizza', FALSE), 'french fries' => array('french fries', FALSE), 'sauerkraut' => array('sauerkraut', FALSE), ); $form=new Forge; $form->input('name')->label(true)->rules('length[0,50]'); $form->input('email')->label(true)->rules('valid_email|required'); $form->checklist('preference')->label(true)->options($foods); $form->textarea('comment')->label(true); $form->submit('submit'); if($form->validate()) { //Load model and set values $preference=new Preference_Model; $preference->name=$form->name->value; $preference->email=$form->email->value; $preference->preference=serialize($form->preference->value); $preference->comment=($form->comment->value); //Save form into table if($preference->save()) { echo 'We\'ve saved it.'; } else { echo 'no saving today'; } } else { echo $form->render(); } }
It should be pretty clear. First I add the elements, then check if it validates if not show the form. If it validates try to save it. Forge handles error messages by itself so that’s easy.
March 22nd, 2008 at 6:16 pm
Great set of tutorials! Thanks for writing these so fast. I’m using Forge on a project now, but will definitely be looking into Formation and the new validation library for later projects.
When you mentioned “Forge has no filters built-in so you can apply them manually”, do you mean simply applying some PHP functions such as trim($form->name->value) or some other method?
March 22nd, 2008 at 8:16 pm
Yes, that would be my approach. Forge also has callbacks which you might be able to use but I haven’t looked into them.
April 11th, 2008 at 12:48 am
I’m a bit confused between Formation and Forge… is formation superceding Forge?
One thing that i found disconcerting with Forge is the error labelling… is there a way to customise the error handling to your own messages?
i.e. ‘Please enter a valid email address’ rather than ‘validation.valid_email’
April 11th, 2008 at 8:42 am
It would be pretty cool if you could write your own method to chain on to the end of the input line… i.e.
$form->input(’email’)->label(true)->rules(’valid_email|required’)->error(’Please enter a valid email address’);
I’m going to have a go at writing an extension.
April 11th, 2008 at 10:45 am
Formation was written for my personal need to handle some stuff a little differently than Forge or the validation library. It is not an official Kohana module. Formation is my personal replacement for Forge and validation and it so happens I released it publicly.
I haven’t looked into Forge’s code for a while. In the old forums you can find an extension to Forge I wrote back then which might be useful to you. I have no clue about the error handling anymore.
Formation does support custom errors for each rule and you can as well declare a separate i18n file for every form. I’m not sure whether I’ve documented it all though.
September 19th, 2008 at 3:02 am
[...] course there are various ways to make this easier, most of which involve some level of automation, but all of which involve writing more, not less, PHP [...]