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

FormHelper::end($options = null, $secureAttributes = array())
The FormHelper includes an end() method that completes the form. Often, end() only outputs a
closing form tag, but using end() also allows the FormHelper to insert needed hidden form elements
that SecurityComponent requires:

<?php echo $this->Form->create(); ?>
<!-- Form elements go here -->
<?php echo $this->Form->end(); ?>

If a string is supplied as the first parameter to end(), the FormHelper outputs a submit button named
accordingly along with the closing form tag:

<?php echo $this->Form->end('Finish'); ?>

Will output:

<div class="submit">
<input type="submit" value="Finish" />
</div>
</form>

You can specify detail settings by passing an array to end():

$options = array(
'label' => 'Update',
'div' => array(
'class' => 'glass-pill',
)
);
echo $this->Form->end($options);

Will output:

<div class="glass-pill"><input type="submit" value="Update" name="Update
Ë“→">
</div>

See the Form Helper API55 for further details

Creating form elements

There are a few ways to create form inputs with the FormHelper. We’ll start by looking at input(). This
method will automatically inspect the model field it has been supplied in order to create an appropriate input
for that field. Internally input() delegates to other methods in FormHelper.

FormHelper::input(string $fieldName, array $options = array())
Creates the following elements given a particular Model.field:

  • Wrapping div.
  • Label element
  • Input element(s)
  • Error element with message if applicable.

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.3. 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')
);

FormHelper::inputs(mixed $fields = null, array $blacklist = null, $options = array())
Generate a set of inputs for $fields. If $fields is null all fields, except of those defined in
$blacklist, of the current model will be used.

In addition to controller fields output, $fields can be used to control legend and fieldset rendering
with the fieldset and legend keys. $this->Form->inputs(array('legend' =>
'My legend'));Would generate an input set with a custom legend. You can customize individual
inputs through $fields as well.

echo $this->Form->inputs(array(
'name' => array('label' => 'custom label')
));

In addition to fields control, inputs() allows you to use a few additional options.

  • fieldset Set to false to disable the fieldset. If a string is supplied it will be used as the class
  • name for the fieldset element.
  • legend Set to false to disable the legend for the generated input set. Or supply a string to customize the legend text.

Field naming conventions

The Form helper is pretty smart. Whenever you specify a field name with the form helper methods, it’ll
automatically use the current model name to build an input with a format like the following:

<input type="text" id="ModelnameFieldname" name="data[Modelname][fieldname]">

This allows you to omit the model name when generating inputs for the model that the form was created for. You can create inputs for associated models, or arbitrary models by passing in Modelname.fieldname as the first parameter:

echo $this->Form->input('Modelname.fieldname');

If you need to specify multiple fields using the same field name, thus creating an array that can be saved in
one shot with saveAll(), use the following convention:

echo $this->Form->input('Modelname.0.fieldname');
echo $this->Form->input('Modelname.1.fieldname');

Output:

<input type="text" id="Modelname0Fieldname"
name="data[Modelname][0][fieldname]">
<input type="text" id="Modelname1Fieldname"
name="data[Modelname][1][fieldname]">

FormHelper uses several field-suffixes internally for datetime input creation. If you are using fields named
year, month, day, hour, minute, or meridian and having issues getting the correct input, you can
set the name attribute to override the default behavior:

echo $this->Form->input('Model.year', array(
'type' => 'text',
'name' => 'data[Model][year]'
));

Options

FormHelper::input() supports a large number of options. In addition to its own options input()
accepts options for the generated input types, as well as HTML attributes. The following will cover the
options specific to FormHelper::input().

  • $options['type'] You can force the type of an input, overriding model introspection, by specifying a type. In addition to the field types found in the Creating form elements, you can also create ‘file’, ‘password’, and any type supported by HTML5:

echo $this->Form->input('field', array('type' => 'file'));
echo $this->Form->input('email', array('type' => 'email'));

Output:

<div class="input file">
<label for="UserField">Field</label>
<input type="file" name="data[User][field]" value="" id="UserField" /
Ë“→>
</div>

<div class="input email">
<label for="UserEmail">Email</label>
<input type="email" name="data[User][email]" value="" id="UserEmail"
Ë“→/>
</div>

  • $options['div'] Use this option to set attributes of the input’s containing div. Using a string value will set the div’s class name. An array will set the div’s attributes to those specified by the array’s keys/values. Alternatively, you can set this key to false to disable the output of the div.

Setting the class name:

echo $this->Form->input('User.name', array(
'div' => 'class_name'
));

Output:

<div class="class_name">
<label for="UserName">Name</label>
<input name="data[User][name]" type="text" value="" id="UserName" />
</div>

Setting multiple attributes:

echo $this->Form->input('User.name', array(
'div' => array(
'id' => 'mainDiv',
'title' => 'Div Title',
'style' => 'display:block'
)
));

Output:

<div class="input text" id="mainDiv" title="Div Title"
style="display:block">
<label for="UserName">Name</label>
<input name="data[User][name]" type="text" value="" id="UserName" />
</div>

Disabling div output:

echo $this->Form->input('User.name', array('div' => false)); ?>

Output:

<label for="UserName">Name</label>
<input name="data[User][name]" type="text" value="" id="UserName" />

  • $options['label'] Set this key to the string you would like to be displayed within the label that usually accompanies the input:

echo $this->Form->input('User.name', array(
'label' => 'The User Alias'
));

Output:

<div class="input">
<label for="UserName">The User Alias</label>
<input name="data[User][name]" type="text" value="" id="UserName" />
</div>

Alternatively, set this key to false to disable the output of the label:

echo $this->Form->input('User.name', array('label' => false));

Output:

<div class="input">
<input name="data[User][name]" type="text" value="" id="UserName" />
</div>

Set this to an array to provide additional options for the label element. If you do this, you can use
a text key in the array to customize the label text:

echo $this->Form->input('User.name', array(
'label' => array(
'class' => 'thingy',
'text' => 'The User Alias'
)
));

Output:

<div class="input">
<label for="UserName" class="thingy">The User Alias</label>
<input name="data[User][name]" type="text" value="" id="UserName" />
</div>

  • $options['error'] Using this key allows you to override the default model error messages and can be used, for example, to set i18n messages. It has a number of suboptions which control the wrapping element, wrapping element class name, and whether HTML in the error message will be escaped.

To disable error message output & field classes set the error key to false:

$this->Form->input('Model.field', array('error' => false));

To disable only the error message, but retain the field classes, set the errorMessage key to false:

$this->Form->input('Model.field', array('errorMessage' => false));

To modify the wrapping element type and its class, use the following format:

$this->Form->input('Model.field', array(
'error' => array(
'attributes' => array('wrap' => 'span', 'class' => 'bzzz')
)
));

To prevent HTML being automatically escaped in the error message output, set the escape suboption
to false:

$this->Form->input('Model.field', array(
'error' => array(
'attributes' => array('escape' => false)
)
));

To override the model error messages use an array with the keys matching the validation rule names:

$this->Form->input('Model.field', array(
'error' => array('tooShort' => __('This is not long enough'))
));

As seen above you can set the error message for each validation rule you have in your models. In
addition you can provide i18n messages for your forms.

  • $options['before'], $options['between'], $options['separator'], and $options['after']

Use these keys if you need to inject some markup inside the output of the input() method:

echo $this->Form->input('field', array(
'before' => '--before--',
'after' => '--after--',
'between' => '--between---'
));

Output:

<div class="input">
--before--
<label for="UserField">Field</label>
--between---
<input name="data[User][field]" type="text" value="" id="UserField" />
--after--
</div>

For radio inputs the ‘separator’ attribute can be used to inject markup to separate each input/label pair:

echo $this->Form->input('field', array(
'before' => '--before--',
'after' => '--after--',
'between' => '--between---',

'separator' => '--separator--',
'options' => array('1', '2'),
'type' => 'radio'
));

Output:

<div class="input">
--before--
<input name="data[User][field]" type="radio" value="1" id="UserField1" />
<label for="UserField1">1</label>
--separator--
<input name="data[User][field]" type="radio" value="2" id="UserField2" />
<label for="UserField2">2</label>
--between---
--after--
</div>

For date and datetime type elements the ‘separator’ attribute can be used to change the string
between select elements. Defaults to ‘-‘.

  • $options['format'] The ordering of the HTML generated by FormHelper is controllable as well. The ‘format’ options supports an array of strings describing the template you would like said element to follow. The supported array keys are array('before','input','between','label','after','error').
  • $options['inputDefaults'] If you find yourself repeating the same options in multiple input() calls, you can use inputDefaults‘ to keep your code dry:

echo $this->Form->create('User', array(
'inputDefaults' => array(
'label' => false,
'div' => false
)
));

All inputs created from that point forward would inherit the options declared in inputDefaults. You
can override the defaultOptions by declaring the option in the input() call:

// No div, no label
echo $this->Form->input('password');
// has a label element
echo $this->Form->input('username', array('label' => 'Username'));

If you need to later change the defaults you can use FormHelper::inputDefaults().

  • $options['maxlength'] Set this key to set the maxlength attribute of the input field to a specific value. When this key is omitted and the input-type is text, textarea, email, tel, url or search and the field-definition is not one of decimal, time or datetime, the length option of the database field is used.
 
 
 

pankaj

Skills    Cakephp

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

  Students (0)