我希望当用户提交表单时,此表单的内容将保存在数据库中,但是它将重定向到具有与上一个字段相同的字段的相同表单,以便用户可以编辑其中一些并保存再来一次。我该如何实现?
代码
add.ctp:
<div class="roles form large-9 medium-8 columns content">
<?= $this->Form->create($role) ?>
<fieldset>
<legend><?= __('Add Role') ?></legend>
<?php
echo $this->Form->control('name');
echo $this->Form->control('description');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
RolesController.php(添加功能)
public function add()
{
$role = $this->Roles->newEntity();
if ($this->request->is('post')) {
$role = $this->Roles->patchEntity($role, $this->request->getData());
if ($this->Roles->save($role)) {
$this->Flash->success(__('The role has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The role could not be saved. Please, try again.'));
}
$this->set(compact('role'));
}
答案 0 :(得分:2)
要保留在同一页面/表格上,只需除去return $this->redirect(['action' => 'index'])
-但我认为这不是您真正想要的。用户将在“添加”页面上死胡同-仅添加更多实体...
签出CakePHP Controller tutorial-它显示了如何创建edit
方法和模板。这样做之后,您必须将重定向更改为return $this->redirect(['action' => 'edit', $role->get('name')])
之类的东西(假设name
属性适合标识角色实体)。