7.3.5 Elementos Específicos de Formulários

O restante dos métodos diponíveis no FormHelper são para criar elementos específicos de formulários. Muitos desses métodos também fazem uso de um segundo parâmetro especial de opções. Nesse caso, o parâmetro $opcoes é usado para especificar elmentos e atributos HTML (como o valor ou o id do DOM do elemento do formulário).

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

A saída será:

 
<input name="data[Usuario][nome]" type="text" class="users" id="UsuarioNome" />

7.3.5.1 checkbox

checkbox(string $fieldName, array $options)

Cria um elemento de form checkbox. Este método também gera uma associação oculta de form input para forçar a submição (submit) do dado para o campo específico.

<?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 $titulo, array $opcoes= array())

Cria um elemento HTML "button" com o título especificado. Definindo o $opcoes['type'] a saída será uma dessas três possibilidades:

  1. button: Cria um botão padrão do HTML.
  2. reset: Cria um botão padrão de limpar o formulário.
  3. submit: Cria um botão padrão de submeter um formulário. (O mesmo que o método $form->submit).
<?php
echo $form->button('Teste');
echo $form->button('Outro Botão', array('type'=>'button'));
echo $form->button('Limpar', array('type'=>'reset'));
echo $form->button('Enviar formulário', array('type'=>'submit'));
?>
  1. <?php
  2. echo $form->button('Teste');
  3. echo $form->button('Outro Botão', array('type'=>'button'));
  4. echo $form->button('Limpar', array('type'=>'reset'));
  5. echo $form->button('Enviar formulário', array('type'=>'submit'));
  6. ?>

A saída será:

<input type="button" value="Teste" />
<input type="button" value="Outro Botão" />
<input type="reset" value="Limpar" />
<input type="Submit" value="Enviar formulário" />

7.3.5.3 year

There is no translation yet for this section. Please help out and translate this.. More information about translations

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

There is no translation yet for this section. Please help out and translate this.. More information about translations

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

There is no translation yet for this section. Please help out and translate this.. More information about translations

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

There is no translation yet for this section. Please help out and translate this.. More information about translations

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

There is no translation yet for this section. Please help out and translate this.. More information about translations

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

There is no translation yet for this section. Please help out and translate this.. More information about translations

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

There is no translation yet for this section. Please help out and translate this.. More information about translations

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

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

7.3.5.10 error

There is no translation yet for this section. Please help out and translate this.. More information about translations

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

There is no translation yet for this section. Please help out and translate this.. More information about translations

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

hidden(string $fieldName, array $options)

Cria um caixa no form input hidden. Exemplo:

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

A saída:

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

7.3.5.13 isFieldError

There is no translation yet for this section. Please help out and translate this.. More information about translations

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

There is no translation yet for this section. Please help out and translate this.. More information about translations

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

There is no translation yet for this section. Please help out and translate this.. More information about translations

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

There is no translation yet for this section. Please help out and translate this.. More information about translations

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

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

Cria um elemento select com as opções do $options, usando o $selected como opção selecionada por padrão. Use $showEmpty como falso se você não quiser mostrar a opção em branco.

<?php
$opcoes=array('M'=>'Masculino','F'=>'Feminino');
echo $form->select('sexo',$opcoes);
?>
  1. <?php
  2. $opcoes=array('M'=>'Masculino','F'=>'Feminino');
  3. echo $form->select('sexo',$opcoes);
  4. ?>

A saída será:

<select name="data[User][sexo]" id="UserSexo">
<option value=""></option>
<option value="M">Masculino</option>
<option value="F">Feminino</option>
</select>

Usando o $showEmpty como falso a saída será:

<select name="data[User][sexo]" id="UserSexo">
<option value="M">Masculino</option>
<option value="F">Feminino</option>
</select>

7.3.5.18 submit

There is no translation yet for this section. Please help out and translate this.. More information about translations

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

There is no translation yet for this section. Please help out and translate this.. More information about translations

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

There is no translation yet for this section. Please help out and translate this.. More information about translations

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>