nuts and bolts of cakephp

Automagical selects or checkboxes

Posted in CakePHP by teknoid on August 8, 2008

A quick example of some automagic goodness…

Let’s say we need to to build an “add Post” form and allow the user to pick some tags for the Post.

In our Posts controller’s add() action, we’d probably do something like this:


$this->set('tags', $this->Post->Tag->find('list', array('fields'=>array('Tag.tag'))));

Then in the view, while building the form, all we need to do is:


echo $form->input('Tag', array('multiple'=>'checkbox'));

Yep, cake will automagically recognize the $tags variable, which we set in the controller, realize that we have Post->Tag models, find the ‘Tag’ field and decide that what we really wanted to do is build a list of checkboxes from our array of $tags.

You can experiment with field names for other cases… things like ‘tags’ or ‘tag_ids’ for the field name should work as well.

12 Responses

Subscribe to comments with RSS.

  1. kiran aghor said, on September 25, 2008 at 11:21 pm

    wow… kool trick. worked for me. thanx a lot. :)

  2. teknoid said, on September 26, 2008 at 9:37 am

    @kiran aghor

    You’re welcome ;)

  3. Gary said, on January 30, 2009 at 9:01 am

    Another great example from you teknoid.

    Gracias

  4. teknoid said, on January 30, 2009 at 9:23 am

    @Gary

    No problem ;)

  5. Vijai Pandey said, on August 26, 2009 at 10:43 am

    Thanks a lot bro…

    I was searching for this for last three hours…..thank you for the post…

    just one update….in my application I just changed one line in views, as described by you

    1. echo $form->input(‘Tag’, array(‘multiple’=>’checkbox’));

    and it worked…no need to add/modify controller (I am using cakephp1.2)

  6. teknoid said, on August 26, 2009 at 11:17 am

    @Vijai Pandey

    Cool. Glad it helped.

  7. Harsha M V said, on October 4, 2009 at 11:42 am

    I have a HABTM association
    Table A, B which is linked via Table A_B

    $form->input(‘area_id’, array(‘class’ => ’short’, ‘div’ => array(‘id’ => ‘area_id’, ‘class’ => ‘area_id’)));

    I ahve a standard list of Areas. How can i get a default value say “Choose Area”
    and how to validate the area field to make sure something is selected other than Choose Area.

  8. teknoid said, on October 5, 2009 at 7:20 pm

    @Harsha M V

    Did you check the manual about the ‘empty’ key?

    I have another post regarding habtm validation, a quick search should give you some hints.

    • Harsha M V said, on October 6, 2009 at 6:41 am

      Yes i have seen about the emty key and checked your post about habtm validation.

      strangely its not working

      echo $form->input(‘area_id’,array(‘class’ => ’short’, ‘div’ => array(‘id’ => ‘area_id’, ‘class’ => ‘area_id’, ‘empty’ => ‘Choose Area’)));

  9. teknoid said, on October 6, 2009 at 9:36 am

    @Harsha M V

    Looking at that code it seems like your ‘empty’ key is in the wrong place (i.e. it’s inside the ‘div’ setting)…
    and class and id attributes have the same name?… a little quirky css :)

    • Harsha M V said, on October 6, 2009 at 10:31 pm

      Yep it was in the wrong place. it works now :D

      abt the same name for class n ID. i still havent applied any CSS rules. its just a temp thing. i am still learning cakephp :D

  10. teknoid said, on October 7, 2009 at 7:22 am

    @Harsha M V

    Cool. Glad everything worked out.


Leave a Reply