Search code examples
cakephpformhelper

CakePHP: difference between "value", "default" and "selected" for options in FormHelper


I'm reviewing the CakePHP documentation and, about forms and the FormHelper, I'm a bit confused about options to set default values ​​for inputs.

From documentation:

Default option (here):

$options['default'] Used to set a default value for the input field. The value is used if the data passed to the form does not contain a value for the field (or if no data is passed at all).

Selected option (here):

$options['selected'] Used in combination with a select-type input (i.e. For types select, date, time, datetime). Set ‘selected’ to the value of the item you wish to be selected by default when the input is rendered:

Later, for FormHelper::select (here):

Creates a select element, populated with the items in $options, with the option specified by $attributes['value'] shown as selected by default.

The only thing of which I am sure, I have to use "checked" for checkboxes:

You cannot use default to check a checkbox - instead you might set the value in $this->request->data in your controller, or set the input option checked to true.

Is there anyone who can explain me clearly how to use these options? Thank you very much.


Solution

  • Value: The content of the value attribute, i.e. <input value="Foo" />

    Default: A default value if none is provided (e.g. in $this->request->data).

    Checked: A checkbox can be checked, which is unrelated to the value attribute, i.e. <input type="checkbox" value="yes" checked="checked" />

    Hope that helps.