【CakePHP3/template】いろんな入力フォームの作成方法
今回は、CakePHP3系で入力フォームを作成する方法を記していきます。
作成方法は、色々ありますが、1種類ずつ紹介していきます。
1.テキストボックスの作成
テキストボックスの作成方法はこちら
index.ctp <?= $this->Form->input('user_name', ['type' => 'text', 'label' => false, 'placeholder' => 'ユーザーネーム']) ?> <?= $this->Form->input({テキストボックス名}, ['type' => 'text', 'label' => {テキストボックスに表示する名前}, 'id' => {ID名}, 'class' => {クラス名}]) ?>
2.チェックボックスの作成
チェックボックスの作成方法はこちら
index.ctp <?= $this->Form->input('checkbox1', ['type' => 'checkbox', 'value' => '1', 'label' => 'checkbox1'])?> <?= $this->Form->input('checkbox2', ['type' => 'checkbox', 'value' => '2', 'label' => 'checkbox2'])?> <?= $this->Form->input('{チェックボックス名}', ['type' => 'checkbox', 'value' => {任意の値}, 'label' => {表示する名前}])?>
3.ラジオボックスの作成
ラジオボックスの作成方法はこちら
index.ctp <?= $this->Form->radio('radio', [1 => '男性', 2 => '女性'])?> <?= $this->Form->radio({ラジオボタン名}, [{値} => {表示名}])?>
4.セレクトボックスの作成
セレクトボックスの作成方法はこちら
index.ctp <?= $this->Form->select('department_id', [1 => '営業部', 2 => '管理部', 3 => 'システム部'], ['empty' => '部署を選択してください。']) ?> <?= $this->Form->select('{セレクトボックス名}', [{選択肢の配列}], ['empty' => {空の場合の表示名}, 'class' => {クラス名}]) ?>
5.テキストエリアの作成
テキストエリアの作成方法はこちら
index.ctp <?= $this->Form->input('textarea', ['type' => 'textarea', 'cols' => 10, 'rows' => 4, 'label' => 'テキストエリア'])?> <?= $this->Form->input({テキストエリア名}, ['type' => 'textarea', 'cols' => {縦の高さ}, 'rows' => {横の長さ}])?>
今回は、各入力フォームを1つずつ紹介しました。
他にもやり方は色々あるので、状況に適した入力フォームを作成してみてください!