Creates the following elements given a particular Model.field
:
The type of input created depends on the column datatype:
- Column Type
Resulting Form Field
- string (char, varchar, etc.)
text
- boolean, tinyint(1)
checkbox
- text
textarea
- text, with name of password, passwd, or psword
password
- text, with name of email
email
- text, with name of tel, telephone, or phone
tel
- date
day, month, and year selects
- datetime, timestamp
day, month, year, hour, minute, and meridian selects
- time
hour, minute, and meridian selects
- binary
file
The $options
parameter allows you to customize how input()
works,
and finely control what is generated.
The wrapping div will have a required
class name appended if the
validation rules for the Model’s field do not specify allowEmpty =>
true
. One limitation of this behavior is the field’s model must have
been loaded during this request. Or be directly associated to the
model supplied to create()
.
New in version 2.5: The binary type now maps to a file input.
Since 2.3 the HTML5 required
attribute will also be added to the input
based on validation rules. You can explicitly set required
key in
options array to override it for a field. To skip browser validation
triggering for the whole form you can set option 'formnovalidate' => true
for the input button you generate using FormHelper::submit()
or
set 'novalidate' => true
in options for FormHelper::create()
.
For example, let’s assume that your User model includes fields for a
username (varchar), password (varchar), approved (datetime) and
quote (text). You can use the input() method of the FormHelper to
create appropriate inputs for all of these form fields:
echo $this->Form->create();
echo $this->Form->input('username'); //text
echo $this->Form->input('password'); //password
echo $this->Form->input('approved'); //day, month, year, hour, minute,
//meridian
echo $this->Form->input('quote'); //textarea
echo $this->Form->end('Add');
A more extensive example showing some options for a date field:
echo $this->Form->input('birth_dt', array(
'label' => 'Date of birth',
'dateFormat' => 'DMY',
'minYear' => date('Y') - 70,
'maxYear' => date('Y') - 18,
));
Besides the specific options for input()
found below, you can specify
any option for the input type & any HTML attribute (for instance onfocus).
For more information on $options
and $htmlAttributes
see
HtmlHelper.
Assuming that User hasAndBelongsToMany Group. In your controller, set a
camelCase plural variable (group -> groups in this case, or ExtraFunkyModel
-> extraFunkyModels) with the select options. In the controller action you
would put the following:
$this->set('groups', $this->User->Group->find('list'));
And in the view a multiple select can be created with this simple
code:
echo $this->Form->input('Group', array('multiple' => true));
If you want to create a select field while using a belongsTo - or
hasOne - Relation, you can add the following to your Users-controller
(assuming your User belongsTo Group):
$this->set('groups', $this->User->Group->find('list'));
Afterwards, add the following to your form-view:
echo $this->Form->input('group_id');
If your model name consists of two or more words, e.g.,
“UserGroup”, when passing the data using set() you should name your
data in a pluralised and camelCased format as follows:
$this->set('userGroups', $this->UserGroup->find('list'));
// or
$this->set(
'reallyInappropriateModelNames',
$this->ReallyInappropriateModelName->find('list')
);
Note
Try to avoid using FormHelper::input() to generate submit buttons. Use
FormHelper::submit()
instead.