Cakephp Classroom image

pankaj
 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13|Last
Lessons:-Views

GET Form Inputs

When using FormHelper to generate inputs for GET forms, the input names will automatically be shortened
to provide more human friendly names. For example:

// Makes <input name="email" type="text" />
echo $this->Form->input('User.email');
// Makes <select name="Tags" multiple="multiple">
echo $this->Form->input('Tags.Tags', array('multiple' => true));

If you want to override the generated name attributes you can use the name option:

// Makes the more typical <input name="data[User][email]" type="text" />
echo $this->Form->input('User.email', array('name' => 'data[User][email]'));

Generating specific types of inputs

In addition to the generic input() method, FormHelper has specific methods for generating a number
of different types of inputs. These can be used to generate just the input widget itself, and combined with
other methods like label() and error() to generate fully custom form layouts.

Common options

Many of the various input element methods support a common set of options. All of these options are
also supported by input(). To reduce repetition the common options shared by all input methods are as
follows:

  • $options['class'] You can set the class name for an input:

echo $this->Form->input('title', array('class' => 'custom-class'));

  • $options['id'] Set this key to force the value of the DOM id for the input.
  • $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).

echo $this->Form->input('ingredient', array('default' => 'Sugar'));

Example with select field (Size “Medium” will be selected as default):

$sizes = array('s' => 'Small', 'm' => 'Medium', 'l' => 'Large');
echo $this->Form->input(
'size',
array('options' => $sizes, 'default' => 'm')
);

Date and datetime fields’ default values can be set by using the ‘selected’ key.

Beware of using false to assign a default value. A false value is used to disable/exclude options of
an input field, so 'default' => false would not set any value at all. Instead use 'default'
=> 0.

In addition to the above options, you can mixin any HTML attribute you wish to use. Any non-special
option name will be treated as an HTML attribute, and applied to the generated HTML input element.

Options for select, checkbox and radio inputs

  • $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:

echo $this->Form->input('close_time', array(
'type' => 'time',
'selected' => '13:30:00'
));

  • $options['empty'] If set to true, forces the input to remain empty.

When passed to a select list, this creates a blank option with an empty value in your drop down list. If
you want to have a empty value with text displayed instead of just a blank option, pass in a string to
empty:

echo $this->Form->input('field', array(
'options' => array(1, 2, 3, 4, 5),
'empty' => '(choose one)'
));

Output:

<div class="input">
<label for="UserField">Field</label>
<select name="data[User][field]" id="UserField">
<option value="">(choose one)</option>
<option value="0">1</option>
<option value="1">2</option>
<option value="2">3</option>
<option value="3">4</option>
<option value="4">5</option>

A list of key-value pairs can be supplied for a date or datetime field:

echo $this->Form->dateTime('Contact.date', 'DMY', '12',
array(
'empty' => array(
'day' => 'DAY', 'month' => 'MONTH', 'year' => 'YEAR',
'hour' => 'HOUR', 'minute' => 'MINUTE', 'meridian' => false
)
)
);

Output:

<select name="data[Contact][date][day]" id="ContactDateDay">
<option value="">DAY</option>
<option value="01">1</option>
// ...
<option value="31">31</option>
</select> - <select name="data[Contact][date][month]" id=
Ë“→"ContactDateMonth">
<option value="">MONTH</option>
<option value="01">January</option>
// ...
<option value="12">December</option>
</select> - <select name="data[Contact][date][year]" id="ContactDateYear
Ë“→">
<option value="">YEAR</option>
<option value="2036">2036</option>
// ...
<option value="1996">1996</option>
</select> <select name="data[Contact][date][hour]" id="ContactDateHour">
<option value="">HOUR</option>
<option value="01">1</option>
// ...
<option value="12">12</option>
</select>:<select name="data[Contact][date][min]" id="ContactDateMin
Ë“→">
<option value="">MINUTE</option>
<option value="00">00</option>
// ...
<option value="59">59</option>
</select> <select name="data[Contact][date][meridian]" id=
Ë“→"ContactDateMeridian">
<option value="am">am</option>
<option value="pm">pm</option>
</select>

  • $options['hiddenField'] For certain input types (checkboxes, radios) a hidden input is created so that the key in $this->request->data will exist even without a value specified:

<input type="hidden" name="data[Post][Published]" id="PostPublished_"
value="0" />
<input type="checkbox" name="data[Post][Published]" value="1"
id="PostPublished" />

This can be disabled by setting the $options['hiddenField'] = false:

echo $this->Form->checkbox('published', array('hiddenField' => false));

Which outputs:

<input type="checkbox" name="data[Post][Published]" value="1"
id="PostPublished" />

If you want to create multiple blocks of inputs on a form that are all grouped together, you should use
this parameter on all inputs except the first. If the hidden input is on the page in multiple places, only
the last group of input’s values will be saved

In this example, only the tertiary colors would be passed, and the primary colors would be overridden:

<h2>Primary Colors</h2>
<input type="hidden" name="data[Color][Color]" id="Colors_" value="0" />
<input type="checkbox" name="data[Color][Color][]" value="5"
id="ColorsRed" />
<label for="ColorsRed">Red</label>
<input type="checkbox" name="data[Color][Color][]" value="5"
id="ColorsBlue" />
<label for="ColorsBlue">Blue</label>
<input type="checkbox" name="data[Color][Color][]" value="5"
id="ColorsYellow" />
<label for="ColorsYellow">Yellow</label>
<h2>Tertiary Colors</h2>
<input type="hidden" name="data[Color][Color]" id="Colors_" value="0" />
<input type="checkbox" name="data[Color][Color][]" value="5"
id="ColorsGreen" />
<label for="ColorsGreen">Green</label>
<input type="checkbox" name="data[Color][Color][]" value="5"
id="ColorsPurple" />
<label for="ColorsPurple">Purple</label>
<input type="checkbox" name="data[Addon][Addon][]" value="5"
id="ColorsOrange" />
<label for="ColorsOrange">Orange</label>

Disabling the 'hiddenField' on the second input group would prevent this behavior.

You can set a different hidden field value other than 0 such as ‘N’:

echo $this->Form->checkbox('published', array(
'value' => 'Y',

'hiddenField' => 'N',
));

Datetime options

  • $options['timeFormat'] Used to specify the format of the select inputs for a time-related setof inputs. Valid values include 12, 24, and null.
  • $options['dateFormat'] Used to specify the format of the select inputs for a date-related setof inputs. Valid values include any combination of ‘D’, ‘M’ and ‘Y’ or null. The inputs will be putin the order defined by the dateFormat option.
  • $options['minYear'],$options['maxYear'] Used in combination with a date/datetime input. Defines the lower and/or upper end of values shown in the years select field.
  • $options['orderYear'] Used in combination with a date/datetime input. Defines the order in which the year values will be set. Valid values include ‘asc’, ‘desc’. The default value is ‘desc’.
  • $options['interval'] This option specifies the number of minutes between each option in the minutes select box:

echo $this->Form->input('Model.time', array(
'type' => 'time',
'interval' => 15
));

Would create 4 options in the minute select. One for each 15 minutes.

  • $options['round'] Can be set to up or down to force rounding in either direction. Defaults to null which rounds half up according to interval.

Form Element-Specific Methods

All elements are created under a form for the User model as in the examples above. For this
reason, the HTML code generated will contain attributes that reference to the User model. Ex:
name=data[User][username], id=UserUsername

FormHelper::label(string $fieldName, string $text, array $options)
Create a label element. $fieldName is used for generating the DOM id. If $text is undefined,
$fieldName will be used to inflect the label’s text:

echo $this->Form->label('User.name');
echo $this->Form->label('User.name', 'Your username');

Output:

<label for="UserName">Name</label>
<label for="UserName">Your username</label>

$options can either be an array of HTML attributes, or a string that will be used as a class name:

echo $this->Form->label('User.name', null, array('id' => 'user-label'));
echo $this->Form->label('User.name', 'Your username', 'highlight');

Output:

<label for="UserName" id="user-label">Name</label>
<label for="UserName" class="highlight">Your username</label>

FormHelper::text(string $name, array $options)
The rest of the methods available in the FormHelper are for creating specific form elements. Many of
these methods also make use of a special $options parameter. In this case, however, $options is used
primarily to specify HTML tag attributes (such as the value or DOM id of an element in the form):

echo $this->Form->text('username', array('class' => 'users'));

Will output:

<input name="data[User][username]" type="text" class="users"
id="UserUsername" />

FormHelper::password(string $fieldName, array $options)
Creates a password field.

echo $this->Form->password('password');

Will output:

<input name="data[User][password]" value="" id="UserPassword"
type="password" />

FormHelper::hidden(string $fieldName, array $options)
Creates a hidden form input. Example:

echo $this->Form->hidden('id');

Will output:

<input name="data[User][id]" id="UserId" type="hidden" />

If the form is edited (that is, the array $this->request->data will contain the information
saved for the User model), the value corresponding to id field will automatically be added to the
HTML generated. Example for data[User][id] = 10:

<input name="data[User][id]" id="UserId" type="hidden" value="10" />

Changed in version 2.0: Hidden fields no longer remove the class attribute. This means that if there
are validation errors on hidden fields, the error-field class name will be applied.
FormHelper::textarea(string $fieldName, array $options)
Creates a textarea input field.

echo $this->Form->textarea('notes');

Will output:

<textarea name="data[User][notes]" id="UserNotes"></textarea>

If the form is edited (that is, the array $this->request->data will contain the information
saved for the User model), the value corresponding to notes field will automatically be added to
the HTML generated. Example:

<textarea name="data[User][notes]" id="UserNotes">
This text is to be edited.
</textarea>

echo $this->Form->textarea('notes', array('escape' => false);
// OR....
echo $this->Form->input(
'notes',
array('type' => 'textarea', 'escape' => false)
);

Options

In addition to the Common options, textarea() supports a few specific options:

  • $options['rows'],$options['cols'] These two keys specify the number of rows and columns:

echo $this->Form->textarea(
'textarea',
array('rows' => '5', 'cols' => '5')
);

Output:

<textarea name="data[Form][textarea]" cols="5" rows="5" id=
Ë“→"FormTextarea">
</textarea>

FormHelper::checkbox(string $fieldName, array $options)
Creates a checkbox form element. This method also generates an associated hidden form input to
force the submission of data for the specified field.

echo $this->Form->checkbox('done');

Will output:

<input type="hidden" name="data[User][done]" value="0" id="UserDone_" />
<input type="checkbox" name="data[User][done]" value="1" id="UserDone" />

It is possible to specify the value of the checkbox by using the $options array:

echo $this->Form->checkbox('done', array('value' => 555));

Will output:

<input type="hidden" name="data[User][done]" value="0" id="UserDone_" />
<input type="checkbox" name="data[User][done]" value="555" id="UserDone"
Ë“→/>

If you don’t want the Form helper to create a hidden input:

echo $this->Form->checkbox('done', array('hiddenField' => false));

Will output:

<input type="checkbox" name="data[User][done]" value="1" id="UserDone" />

FormHelper::radio(string $fieldName, array $options, array $attributes)
Creates a set of radio button inputs.

Options

  • $attributes['value'] to set which value should be selected default.
  • $attributes['separator'] to specify HTML in between radio buttons (e.g. <br />).
  • $attributes['between'] specify some content to be inserted between the legend andfirst element.
  • $attributes['disabled'] Setting this to true or 'disabled' will disable all of the generated radio buttons.
  • $attributes['legend'] Radio elements are wrapped with a legend and fieldset by default. Set $attributes['legend'] to false to remove them.

$options = array('M' => 'Male', 'F' => 'Female');
$attributes = array('legend' => false);
echo $this->Form->radio('gender', $options, $attributes);

Will output:

<input name="data[User][gender]" id="UserGender_" value=""
type="hidden" />
<input name="data[User][gender]" id="UserGenderM" value="M"
type="radio" />
<label for="UserGenderM">Male</label>
<input name="data[User][gender]" id="UserGenderF" value="F"
type="radio" />
<label for="UserGenderF">Female</label>

If for some reason you don’t want the hidden input, setting $attributes['value'] to a selected
value or boolean false will do just that.

  • $attributes['fieldset'] If legend attribute is not set to false, then this attribute can be used to set the class of the fieldset element.

Changed in version 2.1: The $attributes['disabled'] option was added in 2.1.
Changed in version 2.8.5: The $attributes['fieldset'] option was added in 2.8.5.

FormHelper::select(string $fieldName, array $options, array $attributes)
Creates a select element, populated with the items in $options, with the option specified
by $attributes['value'] shown as selected by default. Set the ‘empty’ key in the
$attributes variable to false to turn off the default empty option:

$options = array('M' => 'Male', 'F' => 'Female');
echo $this->Form->select('gender', $options);

Will output:

<select name="data[User][gender]" id="UserGender">
<option value=""></option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>

The select input type allows for a special $option attribute called 'escape' which accepts a
bool and determines whether to HTML entity encode the contents of the select options. Defaults to
true:

$options = array('M' => 'Male', 'F' => 'Female');
echo $this->Form->select('gender', $options, array('escape' => false));

  • $attributes['options'] This key allows you to manually specify options for a select input, or for a radio group. Unless the ‘type’ is specified as ‘radio’, the FormHelper will assume that the target output is a select input:

echo $this->Form->select('field', array(1,2,3,4,5));

Output:

<select name="data[User][field]" id="UserField">
<option value="0">1</option>
<option value="1">2</option>
<option value="2">3</option>
<option value="3">4</option>
<option value="4">5</option>
</select>

Options can also be supplied as key-value pairs:

echo $this->Form->select('field', array(
'Value 1' => 'Label 1',
'Value 2' => 'Label 2',
'Value 3' => 'Label 3'
));

Output:

<select name="data[User][field]" id="UserField">
<option value=""></option>
<option value="Value 1">Label 1</option>
<option value="Value 2">Label 2</option>
<option value="Value 3">Label 3</option>
</select>

If you would like to generate a select with optgroups, just pass data in hierarchical format. This
works on multiple checkboxes and radio buttons too, but instead of optgroups wraps elements in
fieldsets:

$options = array(
'Group 1' => array(
'Value 1' => 'Label 1',
'Value 2' => 'Label 2'
),
'Group 2' => array(
'Value 3' => 'Label 3'
)
);
echo $this->Form->select('field', $options);

Output:

<select name="data[User][field]" id="UserField">
<optgroup label="Group 1">
<option value="Value 1">Label 1</option>
<option value="Value 2">Label 2</option>
</optgroup>
<optgroup label="Group 2">
<option value="Value 3">Label 3</option>
</optgroup>
</select>

  • $attributes['multiple'] If ‘multiple’ has been set to true for an input that outputs a select, the select will allow multiple selections:

echo $this->Form->select(
'Model.field',
$options,
array('multiple' => true)
);

Alternatively set ‘multiple’ to ‘checkbox’ to output a list of related check boxes:

$options = array(
'Value 1' => 'Label 1',
'Value 2' => 'Label 2'
);
echo $this->Form->select('Model.field', $options, array(
'multiple' => 'checkbox'
));

Output:

<div class="input select">
<label for="ModelField">Field</label>
<input name="data[Model][field]" value="" id="ModelField"
type="hidden">
<div class="checkbox">
<input name="data[Model][field][]" value="Value 1"
id="ModelField1" type="checkbox">
<label for="ModelField1">Label 1</label>
</div>
<div class="checkbox">
<input name="data[Model][field][]" value="Value 2"
id="ModelField2" type="checkbox">
<label for="ModelField2">Label 2</label>
</div>
</div>

  • $attributes['disabled'] When creating checkboxes, this option can be set to disable all or some checkboxes. To disable all checkboxes set disabled to true:

$options = array(
'Value 1' => 'Label 1',
'Value 2' => 'Label 2'
);
echo $this->Form->select('Model.field', $options, array(
'multiple' => 'checkbox',
'disabled' => array('Value 1')
));

Output:

<div class="input select">
<label for="ModelField">Field</label>
<input name="data[Model][field]" value="" id="ModelField"
type="hidden">
<div class="checkbox">
<input name="data[Model][field][]" disabled="disabled"
value="Value 1" id="ModelField1" type="checkbox">
<label for="ModelField1">Label 1</label>
</div>
<div class="checkbox">
<input name="data[Model][field][]" value="Value 2"
id="ModelField2" type="checkbox">
<label for="ModelField2">Label 2</label>

</div>
</div>

FormHelper::file(string $fieldName, array $options)
To add a file upload field to a form, you must first make sure that the form enctype is set to
“multipart/form-data”, so start off with a create function such as the following:

echo $this->Form->create('Document', array(
'enctype' => 'multipart/form-data'
));
// OR
echo $this->Form->create('Document', array('type' => 'file'));

Next add either of the two lines to your form view file:

echo $this->Form->input('Document.submittedfile', array(
'between' => '<br />',
'type' => 'file'
));
// OR
echo $this->Form->file('Document.submittedfile');

Due to the limitations of HTML itself, it is not possible to put default values into input fields of type
‘file’. Each time the form is displayed, the value inside will be empty. Upon submission, file fields provide an expanded data array to the script receiving the form data. For the example above, the values in the submitted data array would be organized as follows, if the CakePHP was installed on a Windows server. ‘tmp_name’ will have a different path in a Unix environment:

$this->request->data['Document']['submittedfile'] = array(
'name' => 'conference_schedule.pdf',
'type' => 'application/pdf',
'tmp_name' => 'C:/WINDOWS/TEMP/php1EE.tmp',
'error' => 0,
'size' => 41737,
);

This array is generated by PHP itself, so for more detail on the way PHP handles data passed via file
fields read the PHP manual section on file uploads56.

 
 
 

pankaj

Skills    Cakephp

Qualifications :-
Location :-,,,
Description:-
Explore
 

  Students (0)