Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 388
Mirko Pagliai Avatar asked May 25 '26 00:05

Mirko Pagliai


1 Answers

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.

like image 170
Martin Bean Avatar answered May 28 '26 16:05

Martin Bean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!