Welcome to the Cookbook

loading...

7.3.5 Form Element-Specific Methods

Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen

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).

<?php echo $form->text('username', array('class' => 'users')); ?>
  1. <?php echo $form->text('username', array('class' => 'users')); ?>

Will output:

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

7.3.5.1 checkbox

Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen

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.

<?php echo $form->checkbox('done'); ?>
  1. <?php echo $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" />

7.3.5.2 button

Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen

button(string $title, array $options = array())

Creates an HTML button with the specified title and a default type of "button". Setting $options['type'] will output one of the three possible button types:

  1. button: Creates a standard push button (the default).
  2. reset: Creates a form reset button.
  3. submit: Same as the $form->submit method.
<?php
echo $form->button('A Button');
echo $form->button('Another Button', array('type'=>'button'));
echo $form->button('Reset the Form', array('type'=>'reset'));
echo $form->button('Submit Form', array('type'=>'submit'));
?>
  1. <?php
  2. echo $form->button('A Button');
  3. echo $form->button('Another Button', array('type'=>'button'));
  4. echo $form->button('Reset the Form', array('type'=>'reset'));
  5. echo $form->button('Submit Form', array('type'=>'submit'));
  6. ?>

Will output:

<input type="button" value="A Button" />
<input type="button" value="Another Button" />
<input type="reset" value="Reset the Form" />
<input type="Submit" value="Submit Form" />

7.3.5.3 year

Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen

year(string $fieldName, int $minYear, int $maxYear, mixed $selected, array $attributes, mixed $showEmpty)

Creates a select element populated with the years from $minYear to $maxYear, with the $selected year selected by default. $selected can either be a four-digit year (e.g. 2004) or string 'now'. HTML attributes may be supplied in $attributes.

<?php
echo $form->year('purchased', 2005, 2009);
?>
  1. <?php
  2. echo $form->year('purchased', 2005, 2009);
  3. ?>

Will output:

<select name="data[User][purchased][year]" id="UserPurchasedYear">
<option value=""></option>
<option value="2009">2009</option>
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
<option value="2005">2005</option>
</select>

If $showEmpty is false, the select will not include an empty option. If $showEmpty is a string, it will be used as empty option's name.

<?php
echo $form->year('returned', 2008, 2010, null, null, 'Select a year');
?>
  1. <?php
  2. echo $form->year('returned', 2008, 2010, null, null, 'Select a year');
  3. ?>

Will output:

<select name="data[User][returned][year]" id="UserReturnedYear">
<option value="">Select a year</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
<option value="2008">2008</option>
</select>

7.3.5.4 month

Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen

month(string $fieldName, mixed $selected, array $attributes, boolean $showEmpty)

Creates a select element populated with month names.

<?php
echo $form->month('mob');
?>
  1. <?php
  2. echo $form->month('mob');
  3. ?>

Will output:

<select name="data[User][mob][month]" id="UserMobMonth">
<option value=""></option>
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>

You can pass in your own array of months to be used by setting the 'monthNames' attribute (CakePHP 1.3 only), or have months displayed as numbers by passing false. (Note: the default months are internationalized and can be translated using localization.)

<?php
echo $form->month('mob', null, array('monthNames' => false));
?>
  1. <?php
  2. echo $form->month('mob', null, array('monthNames' => false));
  3. ?>

7.3.5.5 dateTime

Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen

dateTime(string $fieldName, string $dateFormat = ‘DMY’, $timeFormat = ‘12’, mixed $selected, array $attributes, boolean $showEmpty)

Creates a set of select inputs for date and time. Valid values for $dateformat are ‘DMY’, ‘MDY’, ‘YMD’ or ‘NONE’. Valid values for $timeFormat are ‘12’, ‘24’, and ‘NONE’.

7.3.5.6 day

Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen

day(string $fieldName, mixed $selected, array $attributes, boolean $showEmpty)

Creates a select element populated with the (numerical) days of the month.

To create an empty option with prompt text of your choosing (e.g. the first option is 'Day'), you can supply the text as the final parameter as follows:

<?php
echo $form->day('created');
?>
  1. <?php
  2. echo $form->day('created');
  3. ?>

Will output:

<select name="data[User][created][day]" id="UserCreatedDay">
<option value=""></option>
<option value="01">1</option>
<option value="02">2</option>
<option value="03">3</option>
...
<option value="31">31</option>
</select>

7.3.5.7 hour

Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen

hour(string $fieldName, boolean $format24Hours, mixed $selected, array $attributes, boolean $showEmpty)

Creates a select element populated with the hours of the day.

7.3.5.8 minute

Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen

minute(string $fieldName, mixed $selected, array $attributes, boolean $showEmpty)

Creates a select element populated with the minutes of the hour.

7.3.5.9 meridian

Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen

meridian(string $fieldName, mixed $selected, array $attributes, boolean $showEmpty)

Creates a select element populated with ‘am’ and ‘pm’.

7.3.5.10 error

Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen

error(string $fieldName, string $text, array $options)

Shows a validation error message, specified by $text, for the given field, in the event that a validation error has occurred.

Options:

  • 'escape' bool Whether or not to html escape the contents of the error.
  • 'wrap' mixed Whether or not the error message should be wrapped in a div. If a string, will be used as the HTML tag to use.
  • 'class' string The classname for the error message

7.3.5.11 file

Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen

file(string $fieldName, array $options)

Creates a file input.

<?php
echo $form->create('User',array('type'=>'file'));
echo $form->file('avatar');
?>
  1. <?php
  2. echo $form->create('User',array('type'=>'file'));
  3. echo $form->file('avatar');
  4. ?>

Will output:

<form enctype="multipart/form-data" method="post" action="/users/add">
<input name="data[User][avatar]" value="" id="UserAvatar" type="file">
When using $form->file(), remember to set the form encoding-type, by setting the type option to 'file' in $form->create()

7.3.5.12 hidden

Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen

hidden(string $fieldName, array $options)

Creates a hidden form input. Example:

<?php
echo $form->hidden('id');
?>
  1. <?php
  2. echo $form->hidden('id');
  3. ?>

Will output:

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

7.3.5.13 isFieldError

Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen

isFieldError(string $fieldName)

Returns true if the supplied $fieldName has an active validation error.

<?php
if ($form->isFieldError('gender')){
    echo $form->error('gender');
}
?>
  1. <?php
  2. if ($form->isFieldError('gender')){
  3. echo $form->error('gender');
  4. }
  5. ?>
When using $form->input(), errors are rendered by default.

7.3.5.14 label

Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen

label(string $fieldName, string $text, array $attributes)

Creates a label tag, populated with $text.

<?php
echo $form->label('status');
?>
  1. <?php
  2. echo $form->label('status');
  3. ?>

Will output:

<label for="UserStatus">Status</label>

7.3.5.15 password

Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen

password(string $fieldName, array $options)

Creates a password field.

<?php
echo $form->password('password');
?>
  1. <?php
  2. echo $form->password('password');
  3. ?>

Will output:

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

7.3.5.16 radio

Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen

radio(string $fieldName, array $options, array $attributes)

Creates a radio button input. Use $attributes['value'] to set which value should be selected default.

Use $attributes['separator'] to specify HTML in between radio buttons (e.g. <br />).

Radio elements are wrapped with a label and fieldset by default. Set $attributes['legend'] to false to remove them.

<?php
$options=array('M'=>'Male','F'=>'Female');
$attributes=array('legend'=>false);
echo $form->radio('gender',$options,$attributes);
?>
  1. <?php
  2. $options=array('M'=>'Male','F'=>'Female');
  3. $attributes=array('legend'=>false);
  4. echo $form->radio('gender',$options,$attributes);
  5. ?>

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.

7.3.5.17 select

Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen

select(string $fieldName, array $options, mixed $selected, array $attributes, boolean $showEmpty)

Creates a select element, populated with the items in $options, with the option specified by $selected shown as selected by default. Set $showEmpty to false if you do not want an empty select option to be displayed.

<?php
$options=array('M'=>'Male','F'=>'Female');
echo $form->select('gender',$options)
?>
  1. <?php
  2. $options=array('M'=>'Male','F'=>'Female');
  3. echo $form->select('gender',$options)
  4. ?>

Will output:

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

7.3.5.18 submit

Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen

submit(string $caption, array $options)

Creates a submit button with caption $caption. If the supplied $caption is a URL to an image (it contains a ‘.’ character), the submit button will be rendered as an image.

It is enclosed between div tags by default; you can avoid this by declaring $options['div'] = false.

<?php
echo $form->submit();
?>
  1. <?php
  2. echo $form->submit();
  3. ?>

Will output:

<div class="submit"><input value="Submit" type="submit"></div>

You can also pass a relative or absolute url to an image for the caption parameter instead of caption text.

<?php
echo $form->submit('ok.png');
?>
  1. <?php
  2. echo $form->submit('ok.png');
  3. ?>

Will output:

<div class="submit"><input type="image" src="/img/ok.png"></div>

7.3.5.19 text

Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen

text(string $fieldName, array $options)

Creates a text input field.

<?php
echo $form->text('first_name');
?>
  1. <?php
  2. echo $form->text('first_name');
  3. ?>

Will output:

<input name="data[User][first_name]" value="" id="UserFirstName" type="text">

7.3.5.20 textarea

Er is geen nog vertaling vertaling voor deze rubriek. Help ons en vertaal dit.. Meer informatie over vertalingen

textarea(string $fieldName, array $options)

Creates a textarea input field.

<?php
echo $form->textarea('notes');
?>
  1. <?php
  2. echo $form->textarea('notes');
  3. ?>

Will output:

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