The Form tutorial - Forge

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.


9 Responses to “The Form tutorial - Forge”

  1. neovive Says:

    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?

  2. admin Says:

    Yes, that would be my approach. Forge also has callbacks which you might be able to use but I haven’t looked into them.

  3. ed croft Says:

    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’

  4. Ed Croft Says:

    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.

  5. dlib Says:

    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.

  6. Learning Kohana » Easy form population with jQuery and the Populate plugin Says:

    [...] 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 [...]

  7. hexes Says:

    WHEREEEEE!?!?!?
    Where can i get this FORGE module!??!??

  8. spirit Says:

    @hexes: http://code.google.com/p/kohanamodules/source/browse/#svn/tags/2.2/forge

  9. hexes Says:

    2spirit: thx a lot!

Leave a Comment