フォーム
フォームの開始
エンティティの新規登録と編集フォームはどちらも以下のロジックから開始される。
- $articleが空のエンティティの場合、新規登録フォームが生成される。
- $articleが既存のエンティティの場合、エンティティの編集フォームが生成される。
<?php $this->Form->create($article); ?>
上のロジックでフォームの開始タグが生成される。
<form method="post" action="/users/add">
input要素の生成
テキスト
<?php $this->Form->text('username', ['class' => 'users']); // テキストフィールド $this->Form->password('password'); // パスワードフィールド $this->Form->hidden('id'); // 非表示フィールド $this->Form->textarea('comment', ['rows' => '5', 'cols' => '5']); // テキストエリア ?>
このロジックで、それぞれ以下のフォームが出力される。
<input name="username" type="text" class="users"> <input name="password" value="" type="password"> <input name="id" value="(int)" type="hidden" /> <textarea name="comment" cols="5" rows="5"></textarea>
チェックボックス
checkbox('フィールド名', [オプション]);
<?php $this->Form->checkbox('done'); ?>
出力結果
<input type="hidden" name="done" value="0"> <input type="checkbox" name="done" value="1">
セレクトボックス
select('フィールド名', [選択項目を含むオプション配列]);
<?php $this->Form->select( 'field', [1, 2, 3, 4, 5] ); ?>
出力結果
<select name="field"> <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>
オプション配列の選択項目は連想配列でも可。キーがvalue属性、値がラベルの見出しになる。
<?php $this->Form->select('field', [ 'Value 1' => 'Label 1', 'Value 2' => 'Label 2', 'Value 3' => 'Label 3' ]); ?>
<select name="field"> <option value="Value 1">Label 1</option> <option value="Value 2">Label 2</option> <option value="Value 3">Label 3</option> </select>
ラジオボタン
radio('フィールド名', [選択項目を含むオプション配列]);
<?php $this->Form->radio('gender', ['Masculine','Feminine','Neuter']); ?>
出力結果
<input name="gender" value="" type="hidden"> <label for="gender-0"> <input name="gender" value="0" id="gender-0" type="radio"> Masculine </label> <label for="gender-1"> <input name="gender" value="1" id="gender-1" type="radio"> Feminine </label> <label for="gender-2"> <input name="gender" value="2" id="gender-2" type="radio"> Neuter </label>
ファイル入力
file('フィールド名');
<?= $this->Form->create($document, ['type' => 'file']); ?> // まずフォーム開始時にtypeをfileに指定 <?= $this->Form->file('submittedfile'); ?>
日時選択
<?php $this->Form->dateTime('published'); // 日時 $this->Form->date('registered', [ // 日付 'minYear' => 2018 ]); $this->Form->time('released', [ // 時間 'interval' => 15 ]); $this->Form->year('published', [ // 年 'minYear' => 2000, 'maxYear' => date('Y') ]); $this->Form->month('published'); // 月 $this->Form->day('published'); // 日 $this->Form->hour('created', [ // 時 'format' => 24 ]); $this->Form->minute('arrival', [ // 分 'interval' => 10 ]); ?>
ラベルの生成
label('フィールド名', 見出しのオプション文字列', [オプション配列]);
<?php $this->Form->label('name', 'Your username', ['class' => 'highlight']); ?>
出力結果
<label for="name" class="highlight">Your username</label>
エラーメッセージの表示
error('フィールド名', 'エラーメッセージの文字列or配列');
<?php $this->Form->error('ticket', 'Completely custom error message!'); ?>
出力結果
<div class="error-message">Completely custom error message!</div>
ボタンの生成
送信ボタン
submit('ボタンの見出し');
<?php $this->Form->submit('Submit!'); ?>
出力結果
<div class="submit"><input value="Submit!" type="submit"></div>
ボタン全般
button('ボタンの見出し', [オプション配列]);
<?php $this->Form->button('標準のボタン', ['type' => 'button']); $this->Form->button('フォームのリセット', ['type' => 'reset']); $this->Form->button('フォームの送信', ['type' => 'submit']); ?>
出力結果
<button type="button">別のボタン</button> <button type="reset">フォームのリセット</button> <button type="submit">フォームの送信</button>
フォームの終了
<?php $this->Form->end(); ?>
フォームのオプション
type
作成するフォームのHTTPメソッドを指定。'get', 'post', 'put', 'patch', 'delete'が指定できる。
<?php $this->Form->create($article, ['type' => 'get']); ?>
url
フォームを送信するURLを指定。フォームを特定のアクションに向ける。
<?php $this->Form->create($article, ['url' => ['action' => 'publish']]); ?>
目的のアクションが現在のコントローラにない場合。
<?php $this->Form->create(null, [ 'url' => [ 'controller' => 'Articles', 'action' => 'publish' ] ]); ?>
外部ドメインを指定する場合。
<?php $this->Form->create(null, [ 'url' => 'http://www.google.com/search', 'type' => 'get' ]); ?>
validator
フォームにカスタムバリデーターを適用させる。
<?php $this->Form->create($user, [ 'context' => ['validator' => 'register'] ]); ?>
上の例では、UsersTable::validationRegister() で定義されている register バリデーターを$userに適用する。