CakePHP View操作 Formヘルパー

フォーム

<?= $this->Form->create(
(モデル名)/null,
[
“type”=>”file”/”get”/”post”
‘url’ => [
‘controller’ => (コントローラ名),
‘action’ => (アクション名),
],
]
)
?>

<?= $this->Form->create(‘test’, [
“type”=>”post”,
‘url’ => [
‘controller’ => ‘testcontroller’,
‘action’ => ‘testaction’,
]
])?>


<?= $this->Form->end() ?>

テキスト

通常

<?=$this->Form->text(文字列) ?>

指定文字数で抜粋

<?=$this->Form->text->excerpt(文字列, ‘method’, 文字数, ‘…’) ?>

ラベル

<?=$this->Form->label(name属性, 文字列) ?>

テキストエリア

<?=$this->Form->textarea(
文字列,
[
‘rows’=>’10’,
‘cols’=>’10’
]
) ?>

ラジオボタン

<?=$this->Form->radio(
文字列,
[
[‘text’=>’表示’, ‘value’=>’1’],
[‘text’=>’非表示’, ‘value’=>’0’, ‘checked’ => true]
[‘default’ => 1]
]
) ?>

セレクトボックス

<?= $this->Form->input(文字列, [
‘type’ => ‘select‘,
‘options’ =>
[
[‘text’=>’1,000円’, ‘value’=>’1000’],
[‘text’=>’2,000円’, ‘value’=>’2000’],
[‘text’=>’3,000円’, ‘value’=>’3000’],
],
空白表示
‘empty’ => true,
初期値
‘default’ => -1,
‘label’ => ”]) ?>

DB値を表示

※コントローラ
$this->Test = TableRegistry::get(‘test’);
$test = $this->Test
->find()
->select([‘id’, ‘test_nm’])
->all()
->combine(‘id’, ‘test_nm’)
->toArray();
$this->set(compact(‘test‘));
ビュー
<?= $this->Form->select(
“test”,
$test,
[‘default’=>$test[‘id’]]
)
?>

チェックボックス

<?= $this->Form->input(‘dl_flg’,
[
‘type’ => ‘checkbox‘,
‘value’ => ‘1’,
‘label’ => ‘ダウンロード’,
‘checked’ => true,
‘hiddenField’ => false
]
); ?>

ポストリンク

<?= $this->Form->postLink( 文字列, ‘/コントローラ名/メソッド名’ ) ?>

hidden

<?= $this->Form->hidden(文字列 , [‘value’ => 123 ]) ?>

ファイルアップロード

<?=$this->Form->file(文字列) ?>

汎用

メールアドレス

<?= $this->Form->control(“name”, [‘type’=>’email’, ‘value’=~, ‘label’=>false, ‘placeholder’=>’sample@sample.com’, ‘required’=>’required’]) ?>

パスワード

<?= $this->Form->control(“name”, [‘type’=>’password’, ‘value’=~, ‘label’=>false, ‘required’=>’required’]) ?>

SUBMIT

<?=$this->Form->submit() ?>
複数のSubmitで処理を分ける場合
<?= $this->Form->submit(“編集”, [‘name’=>’edit’]) ?>
<?= $this->Form->submit(“削除”, [‘name’=>’delete’]) ?>
※コントローラ
if (isset($this->request->getData()[‘edit’])) {

}else if (isset($this->request->getData()[‘delete’]) ) {

}