使用多个控制器函数cakephp预先填写多个表单并在一个视图上提交
我正在用cakePHP构建一个应用程序(一般来说,我不熟悉编码),我希望在一个视图上有多个表单(edit.ctp),表单使用单独的控制器函数。我环顾四周,不确定如何实现这一点,因为一个控制器函数几乎与视图文件绑定在一起,其名称与控制器方法相同。以下是我的控制器方法:使用多个控制器函数cakephp预先填写多个表单并在一个视图上提交,php,forms,cakephp,view,controller,Php,Forms,Cakephp,View,Controller,我正在用cakePHP构建一个应用程序(一般来说,我不熟悉编码),我希望在一个视图上有多个表单(edit.ctp),表单使用单独的控制器函数。我环顾四周,不确定如何实现这一点,因为一个控制器函数几乎与视图文件绑定在一起,其名称与控制器方法相同。以下是我的控制器方法: public function edit($id = null) { if (!$id) { throw new NotFoundException(__('Invalid User')); }
public function edit($id = null) {
if (!$id) {
throw new NotFoundException(__('Invalid User'));
}
if(!$user) {
throw new NotFoundException(__('Invalid User'));
}
$user = $this->Users->get($id);
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
public function editProfile($id = null)
{
$user = $this->Users->get($id);
if (!$id) {
throw new NotFoundException(__('Invalid User'));
}
if(!$user) {
throw new NotFoundException(__('Invalid User'));
}
if ($this->request->is(['patch', 'post', 'put'])) {
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('Your profile has been editted.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Your profile could not be editted. Please, try again.'));
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
$this->view = ('edit');
}
public function changePassword()
{
if (!$id) {
throw new NotFoundException(__('Invalid User'));
}
$password = $this->Users->get($id);
if(!$password) {
throw new NotFoundException(__('Invalid User'));
}
if ($this->request->is(['patch', 'post', 'put'])) {
if ( password_verify($this->request->data('old_password'), $password->password) === true && password_verify($this->request->data('password'), $password->password) === false ) {
$password = $this->Users->patchEntity($password, $this->request->data);
if ($this->Users->save($password)) {
$this->Flash->success(__('Your profile has been editted.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Your profile could not be editted. Please, try again.'));
} elseif ( password_verify($this->request->data('old_password'), $password->password) === false ) {
$this->Flash->error(__('Your old password is not correct.'));
} elseif ( password_verify($this->request->data('old_password'), $password->password) === true ) {
$this->Flash->error(__('You must enter a password different from your current one.'));
} else {
$this->Flash->error(__('Your profile could not be editted. Please, try again.'));
}
}
$this->set(compact('password'));
$this->set('_serialize', ['password']);
$this->view = ('edit');
}
以下是我的看法:
//users/edit.ctp
<div class="page-wrap">
<div class="form">
<h1>Edit Your Profile</h1>
<?= $this->Form->create(null, array('url'=>array('controller' => 'users', 'action' => 'editProfile'))) ?>
<?= $this->Form->input('full_name', ['value' => $user->full_name]) ?>
<?= $this->Form->input('email', ['value' => $user->email]) ?>
<?= $this->Form->button(__('Save')) ?>
<?= $this->Form->end() ?>
<?= $this->Form->create(null, array('url'=>array('controller' => 'users', 'action' => 'changePassword'))) ?>
<fieldset>
<legend>Change Your Password</legend>
<?= $this->Form->input('old_password', ['type' => 'password', 'value' => '']) ?>
<?= $this->Form->input('password', ['label' => 'New Password', 'type' => 'password', 'value' => '']) ?>
<?= $this->Form->input('password_confirmation', ['label' => 'New Password Confirmation', 'type' => 'password', 'value' => '']) ?>
</fieldset>
<?= $this->Form->button(__('Change Password')) ?>
<?= $this->Form->end() ?>
<p><?= $this->Html->link('Back', ['controller' => 'users', 'action' => 'index']); ?></p>
</div>
</div>
//用户/edit.ctp
编辑您的个人资料
更改密码
主要问题是,$user/$password变量没有从editProfile和changePassword方法提交到edit.ctp视图,因此它会抛出一个未定义变量的错误,因此我尝试在edit函数中设置它,它消除了错误,但是我无法获得要保存的信息。其他方法无法获取提交数据的id(在主键为[NULL]的表“users”中找不到记录)我找到了一个快速修复方法,将控制器函数changePassword变量$password更改为$user,然后将$this->Form->create中的null改为$this->Form->create,我将$user放在这两个表单中,因为它们提交给控制器中的两个不同函数,所以仍然有效。几乎所有表单助手的功能都是可用的可配置。create方法采用了许多有用的选项,url提交就是其中之一。您还可以将其设置为post到diff action和controller。