我正在用cakePHP构建一个应用程序(我是一般的编码新手),我希望在一个视图(edit.ctp)上有多个表单,其中包含使用单独控制器函数的表单。我环顾四周,并不知道如何实现这一点,因为一个控制器函数几乎与视图文件绑定,其名称与控制器方法相同。这是我的控制器方法:
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>
主要问题是$ user / $ password变量没有从editProfile和changePassword方法提交到edit.ctp视图,所以它抛出了一个未定义变量的错误,所以我试着在编辑函数中设置它它摆脱了错误,但后来我无法获取要保存的信息。其他方法没有获取提交数据的ID(记录未在表中找到&#34;用户&#34;主键为[NULL])
答案 0 :(得分:0)
通过将控制器函数changePassword变量$ password更改为$ user,我找到了一个快速解决方法,然后将$ null放在$ this-&gt; Form-&gt;中创建我将$ user放在两个表单中,因为它们是提交到控制器中的两个不同的功能它仍然有效。