CakePHP

【備忘録】bake後、確認画面をサクッと作る

More than 3 years have passed since last update.

いつかbakeを改良してやろうと思うのですが、それまでは手で作る必要があり、備忘録として以下を作成しました


add.ctp(必須)

// 次の行を追加

echo $this->Form->hidden('mode',array('value' => 'confirm'));


add.ctp(任意)

// $this->Form->end(__('Submit'));

$this->Form->end(__('Next'));


edit.ctp(必須)

// 次の行を追加

echo $this->Form->hidden('mode',array('value' => 'edit_confirm'));


edit.ctp(任意)

// $this->Form->end(__('Submit'));

$this->Form->end(__('Next'));


confirm.ctp(新規作成する)

<div class="単数モデル名 form">

<?php echo $this->Form->create('単数モデル名'); ?>
<fieldset>
<legend><?php echo "入力内容をご確認ください"; ?></legend>
<dl>
<dt><?php echo __("Subject"); ?>&nbsp;</dt>
<dd><?php echo $this->Form->value("subject"); ?>&nbsp;</dd>
</dl>
<?php
echo $this->Form->hidden('subject' , array('value' => $this->Form->value("subject") ) )."\n";
?>
</fieldset>
<?php echo $this->Form->end(__('Finish')); ?>
</div>


edit_confirm.ctp

// confirm.ctpをコピーして作成し、次の行を追加する

echo $this->Form->hidden('id' , array('value' => $this->Form->value("id") ) )."\n";


controller.php

    public function add() {

if ($this->request->is('post')) {
if ($this->request->data['単数モデル名']['mode'] == "confirm" ){
$this->render("confirm");
} else{
$this->単数モデル名->create();
if ($this->単数モデル名->save($this->request->data)) {
$this->Session->setFlash(__('The 単数モデル名 has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The 単数モデル名 could not be saved. Please, try again.'));
}
}
}
}

public function edit($id = null) {
if (!$this->単数モデル名->exists($id)) {
throw new NotFoundException(__('Invalid 単数モデル名'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->request->data['単数モデル名']['mode'] == "edit_confirm" ){
$this->render("edit_confirm");
} else{
if ($this->単数モデル名->save($this->request->data)) {
$this->Session->setFlash(__('The 単数モデル名 has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The 単数モデル名 could not be saved. Please, try again.'));
}
}
} else {
$options = array('conditions' => array('単数モデル名.' . $this->単数モデル名->primaryKey => $id));
$this->request->data = $this->単数モデル名->find('first', $options);
}
}