Welcome to the Cookbook

loading...

7.3.5 Form Element-Specific Methods

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

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

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

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

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

Erstellt eine Schaltfläche mit Titel und einem Standardtyp. Mit der Option $options['type'] können drei verschiedene Typen für die Schaltfläche angegeben werden.

  1. button: Es wird eine Standardschaltfläche erstellt.
  2. reset: Es wird eine Reset Schaltfläche erstellt.
  3. submit: Es wird eine Schaltfläche, für welche auch der Code $form->submit angewendet werden kann, erstellt.
<?php
echo $form->button('Schaltfläche');
echo $form->button('andere Schaltfläche', array('type'=>'button'));
echo $form->button('Formular resetten', array('type'=>'reset'));
echo $form->button('Formular absenden', array('type'=>'submit'));
?>
  1. <?php
  2. echo $form->button('Schaltfläche');
  3. echo $form->button('andere Schaltfläche', array('type'=>'button'));
  4. echo $form->button('Formular resetten', array('type'=>'reset'));
  5. echo $form->button('Formular absenden', array('type'=>'submit'));
  6. ?>

Entspricht der HTML-Ausgabe:

<input type="button" value="Schaltfläche" />
<input type="button" value="andere Schaltfläche" />
<input type="reset" value="Formular resetten" />
<input type="Submit" value="Formular absenden" />

7.3.5.3 year

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

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

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

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

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

Erstellt einen Satz Dropdowns für Datum und Zeit. Gültige Werte für $dateformat sind ‘DMY’, ‘MDY’, ‘YMD’ und ‘NONE’. Gültige Werte für $timeFormat sind ‘12’, ‘24’, und ‘NONE’.

7.3.5.6 day

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

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

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

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

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

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

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

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

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

7.3.5.10 error

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

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

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

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

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

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

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

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

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

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

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

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

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

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

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

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

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

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

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

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

Es gibt zur Zeit keine Übersetzung für diesen Abschnitt. Bitte hilf mit und übersetze ihn. Mehr Informationen zu Übersetzungen

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>