【问题标题】:Pre-filling Multiple Forms and submitting on one view with multiple controller functions cakephp预填充多个表单并使用多个控制器功能在一个视图上提交 cakephp
【发布时间】:2015-05-28 17:22:31
【问题描述】:

我正在使用 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(在表“users”中找不到主键[NULL]的记录)

【问题讨论】:

    标签: php forms cakephp view controller


    【解决方案1】:

    我通过将控制器函数 changePassword 变量 $password 更改为 $user 找到了一个快速解决方法,然后我没有将 null 放在 $this->Form->create 中,而是将 $user 放在两个表单中,因为它们正在提交给控制器中的两个不同功能仍然有效。

    【讨论】:

    • 几乎所有表单助手的功能都是可配置的。 create 方法有许多有用的选项,url 提交为一个。你也可以将它设置为 diff 动作和控制器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多