Modified Muliple Checkbox Helper

I’ve been getting some very odd results with Joshua McFarrens excellent checkbox helper and ended up having to modify it slightly to get it to work. Here is my revised version:

'Text' pairs) 
     * @param array $selected Selected checkboxes 
     * @param string $inbetween String that separates the checkboxes. 
     * @param array $htmlAttributes Array of HTML options 
     * @param  boolean $return         Whether this method should return a value 
     * @return string List of checkboxes 
    function checkboxMultiple($fieldName, $options, $selected = null, $inbetween = null, $htmlAttributes = null, $return = false) 
       		 if ($this->tagIsInvalid($this->model, $this->field)) 
            		if (isset($htmlAttributes['class']) && trim($htmlAttributes['class']) != "") 
                			$htmlAttributes['class'] .= ' form_error'; 
                			$htmlAttributes['class'] = 'form_error'; 
        if (!is_array($options)) 
            	return null; 
        if (!isset($selected)) 
				$selected = $this->tagValue($fieldName); 
		while(list($key, $name) = each($options))
				$optionsHere = $htmlAttributes; 
				if(in_array($key, $selected))
						$optionsHere['checked'] = 'checked'; 
				$optionsHere['value'] = $key; 
            	$checkbox[] = "
  • " . sprintf($this->tags['checkboxmultiple'], $this->model, $this->field, $this->parseHtmlOptions($optionsHere), $name) . "
  • \n"; } return "\n" . sprintf($this->tags['hiddenmultiple'], $this->model, $this->field, null, $name) . "\n
      \n" . $this->output(implode($checkbox), $return) . "
    \n"; } } ?>

    (The difference between this and the original version is that the foreach loop has been replaced with a while(list loop that works in slightly different way.)

    6 thoughts to “Modified Muliple Checkbox Helper”

    1. I’ve been looking all over the web…and am disappointed at a lack of CakePHP 1.2 tutorials. Many new items, including form helpers. I downloaded your .zip and modified the templates to .ctp extensions. Under app/views/posts, I modified the add.ctp file this way:

      New Post
      create(‘Post’); ?>

      input(‘title’, array(‘label’ => ‘Title’, ‘size’ => ’20’, ‘class’ => ‘required’)); ?>
      error(‘Post.title’, ‘Please enter the Title.’); ?>

      input(‘body’, array(‘cols’ => ’60’, ‘rows’ => ’10’, ‘class’ => ‘required’)); ?>
      error(‘Post.body’, ‘Please enter the Body.’); ?>

      input(‘status’, null); ?>
      error(‘Post.status’, ‘Please check the Status.’); ?>

      label(‘Tag.tag’,’Related Tags’,null); ?>
      checkboxMultiple(‘Tag.Tag’, $tags, $selectedTags, array(‘multiple’ => ‘multiple’, ‘class’ => ‘selectMultiple’));?>
      error(‘Tag.tag’, ‘Please check the Related Tags.’); ?>

      end(‘Add’); ?>

      link(‘List Posts’, ‘/posts/index’)?>


      But i get these errors:

      Notice (8): Undefined property: HabtmHelper::$model [CORE\app\views\helpers\habtm.php, line 19]

      Context | Code

      $fieldName = “Tag.Tag”
      $options = false
      $selected = null
      $inbetween = array(“multiple” => “multiple”, “class” => “selectMultiple”)
      $htmlAttributes = null
      $return = false



      if ($this->tagIsInvalid($this->model, $this->field))


      HabtmHelper::checkboxMultiple() – CORE\app\views\helpers\habtm.php, line 19
      include – CORE\app\views\posts\add.ctp, line 14
      View::_render() – CORE\cake\libs\view\view.php, line 772
      View::render() – CORE\cake\libs\view\view.php, line 337
      Controller::render() – CORE\cake\libs\controller\controller.php, line 663
      PostsController::add() – CORE\app\controllers\posts_controller.php, line 24
      Dispatcher::_invoke() – CORE\cake\dispatcher.php, line 353
      Dispatcher::dispatch() – CORE\cake\dispatcher.php, line 333
      [main] – CORE\app\webroot\index.php, line 83

      Notice (8): Undefined property: HabtmHelper::$field [CORE\app\views\helpers\habtm.php, line 19]

    2. I have to say I agree with you – I would like to see more stuff about 1.2 – there is a lot about (I think) but it is all buried in groups etc.

      Whilst groups (etc.) are great, the trouble is you have to know what you are looking for – if you have a manual or a set of tutorials you can just scan through them and find things out you don’t have to know what you are after… you can just stumble upon things.

      To quote the un likely poet Donald Rumsfeld:

      …because as we know, there are known knowns; there are things we know we know. We also know there are known unknowns; that is to say we know there are some things we do not know. But there are also unknown unknowns — the ones we don’t know we don’t know.

      With regards to you error – I’m not sure as I haven’t done anything with 1.2 – but at some point I’ll be making the move so would appreciate if you post somewhere what is going wrong.


    3. I’ve been thinking about your problem some more – do you actually need to do this in 1.2 ? – I was under the impression (though could be wrong) that there was proper HABTM / checkbox support within it.

      It certainly has the ability to use criteria in the join table for HABTM using WITH – the lack of which is really frustrating in 1.1

    4. I’d like to do this in 1.2, I suppose it is a feature in 1.2, but without a bit of concrete examples, not sure how to do it.

    5. For now I’m scrapping learning Cake 1.2. Not enough documentation yet. I’m working on learning CodeIgniter 1.5.4.

    6. Hi Spheroid

      I know where you’re coming from – I don’t feel particularly like diving into Cake 1.2 at the moment. Right now I’ve completed 3 Cake based CRM systems and I’m currently working on a largeish (~ 400 pages at launch) website also Cake powered.

      It’s only now on the website that I feel like I really understand the way Cake works and can now start to get real benefits out of it. To tell the truth building the 3 CRMs was a bit of slog at times – and I’m glad that they are private systems and are locked down tight. If you build something from scatch with your own code – bits might be badly written but at least you can hold an overview in your head – you know exactly how the security works for example…

      With Cake – you have this potentailly powerful framework, but I felt I needed to do a suprising amount of work to get it to a position where I can really get going with it (security, uploads, assorted helpers etc.)

      Anyway I’ve never used CodeIgniter – I briefly looked at it – but decieded to go down the Cake root (because of the docs). I’d be interested to know how you get on…

    Comments are closed.