CakePHP fieldsets to keep your forms organized

Friday, 20 March 2009
Tagged: Website, General

This one is pretty simple, but useful for keeping things organised and easy to understand for your users when going through longer form sets.

The fieldset element isn't used as much as it should be. Its great for grouping content, and allowing a mental breakup for the user to make forms easier to comprehend. Guess what, CakePHP makes it super easy to implement!

I see a lot of new bakers using $form->input('my_field'), which is not a problem, but you can really reduce your development time if you push all the input fancyness to CSS, and use the bulk input output (yes, it outputs inputs) mechanism on the FormHelper.

One of the configurable options is 'legend', which will label a fieldset for the inputs you are displaying, like so:

<php echo $form->inputs(array( 'legend' => 'Personal information', 'name', 'nickname', 'age', 'email')); echo $form->inputs(array( 'legend' => 'Employment information', 'company', 'started_work', 'description')); ?>

Give this a go, and get on to some good CSS templates / designs for getting your fieldsets all organised and pretty.

4 Comments

Dan

Dan

Wednesday, 15 April 2009

Thanks!

You can also add options after each field declaration ie... " => array('maxLength' => '3')"

Jean-Philippe Sirois

Jean-Philippe Sirois

Monday, 29 June 2009

Thanks for the advice, putting all the inputs in the same array is also a good way to avoid having tons of echo and repeat useless code like "echo $form->input". Just a little thing I would like to warn you about. The PHP opening tag of your exemple didn't have the "?".

Thanks again for the great tips

Matbaa

Matbaa

Tuesday, 14 July 2009

Thanks. But It should be more useful. So I think

Legend name writing is preferable.

della

della

Wednesday, 17 March 2010

great post. thanks.

Comment on this post