This CakePHP example will show you how to merge your Add and Edit forms into a Single Form action.
This may be of benefit if you have a form with complex controller logic that you don't want to duplicate.
The Controller
We redirect the add and edit actions to use the form action.
controllers/posts_controller.phpclass PostsController extends AppController {
var $name = 'Posts';
function add() {
$this->admin_form();
$this->render('form');
}
function edit($id = null) {
if (!$id && empty($this->data)) {
$this->_flash(__('Invalid Post.', true),'error');
$this->redirect(array('action'=>'index'));
}
$this->admin_form($id);
$this->render('form');
}
function form($id = null) {
if (!empty($this->data)) {
$this->Post->create();
if ($this->Post->save($this->data)) {
$this->_flash(__('The Post has been saved.', true),'success');
$this->redirect(array('action'=>'view',$this->Video->id));
}
else {
$this->_flash(__('The Post could not be saved. Please, try again.', true),'warning');
}
}
if (empty($this->data)) {
$this->data = $this->Post->read(null, $id);
}
}
}
var $name = 'Posts';
function add() {
$this->admin_form();
$this->render('form');
}
function edit($id = null) {
if (!$id && empty($this->data)) {
$this->_flash(__('Invalid Post.', true),'error');
$this->redirect(array('action'=>'index'));
}
$this->admin_form($id);
$this->render('form');
}
function form($id = null) {
if (!empty($this->data)) {
$this->Post->create();
if ($this->Post->save($this->data)) {
$this->_flash(__('The Post has been saved.', true),'success');
$this->redirect(array('action'=>'view',$this->Video->id));
}
else {
$this->_flash(__('The Post could not be saved. Please, try again.', true),'warning');
}
}
if (empty($this->data)) {
$this->data = $this->Post->read(null, $id);
}
}
}
The View
The view can be copied from your current edit template.
views/posts/form.ctp