Archive for the ‘Helpers’ Category

Open a Form

Aug 15, 2007

There isn’t really a helper method to create a form in Cake 1.1.X; it is deprecated. To create a form the easiest way would be to write:

form action=”<?php echo $html->url(‘/’ . $this->controller->name . ‘/’ . $this->controller->action)?>” method=”post”>

Advertisements

Checkbox and Database

Aug 14, 2007

To make life somewhat easier, database columns that will be rendered as checkboxes should be of type tinyint (or number) with values of zero and one (where 1 is checked).

The definition of the html->checkbox function is (note: title is not used):

checkbox($fieldName, $title = null, $htmlAttributes = array(), $return = false)

Whether or not the checkbox is checked is based on the htmlAttributes array. To illustrate what I mean, these $htmlAttribute arrays will result in a checked checkbox:

array('value'=>1)
array('value'=>0)
array('checked'=>1)
array('checked'=>0)
array('checked'=>'checked')

A bit of odd code does make the value of 0 (zero) also check the checkbox, so do not do this

echo $html->checkbox('Model/column', null, array('value'=>$ind));

and expect the checkbox to be unchecked if $ind is zero. It seems this would be the right thing to do to optimize your code, however, it does not work as expected.

So, to create a checkbox you’d write the easiest thing possible:

echo $html->checkbox('Model/column');

and CakePHP will make it checked for you (no extra code required).

echo HTML Helper output

Aug 14, 2007

If you do not have AUTO_OUTPUT set to TRUE in /cake/app/config/core.php then be sure to echo all HTML helper methods.

Example:

echo $html->input('Model/column', array('size'=>'1', 'maxlength'=>'1'));

This is also true when rendering elements (which is also done in the View):

echo $this->renderElement('menu');