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

StackOverflow https://stackoverflow.com/questions/19008956

  •  29-06-2022
  •  | 
  •  

Question

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.

Was it helpful?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top