dongpo4197 2011-06-29 15:12
浏览 35
已采纳

表单操作在验证错误后丢失了帖子ID - CakePHP

My edit action is working great if I don't have any validation errors.

When validation errors are found, the view is rendered again with the error messages, but the URL in the form tag's action looses the post id.

e.g.

Before validation errors:

<form class="niceInput" id="PostEditForm" method="post" action="/posts/edit/1" accept-charset="utf-8">

After validation errors:

<form class="niceInput" id="PostEditForm" method="post" action="/posts/edit" accept-charset="utf-8">

What could be causing this?

Thanks


EDIT: Added 'edit' method in posts_controller.php

    function edit($id) {
    // $id = $this->params['pass'][0];
    $this->set('title_for_layout', 'Edit post');
    $this->Post->id = $id;  
    $this->Post->user_id = $this->Session->read('Auth.User.id');

    $postUserId = $this->Post->read('user_id', $id);

    // check if user logged in owns the post
    if ($this->Auth->user('id') != $postUserId['Post']['user_id']) {
        $this->redirect('/posts/manage');
    }   

    if (empty($this->data)) {
        $this->data = $this->Post->read();
    } else {
        if ($this->Post->save($this->data)) {

            if (!empty($this->data['Post']['image_files'])){ 
                $this->_moveImages($this->data);
            }

            $this->Session->setFlash('Your post has been updated.');
            $this->redirect('/posts/manage');
        }
    }
    $this->render('add');

EDIT 2: add.ctp view

if ($this->action == 'edit') {
    echo $this->Form->create('Post', array('class' => 'niceInput', 'action' => 'edit'));
} else {
    echo $this->Form->create('Post', array('class' => 'niceInput'));
}


echo $this->Form->input('type', array(
                        'label' => 'Type of post',
                        'type' => 'select',
                        'options' => array(
                            'rent' => 'Rental',
                            'roommate' => 'Roommate',
                            'sublet' => 'Sublet'
                        )));
echo $this->Form->input('street_address', array('label' => 'Street address'));
echo $this->Form->input('city');
echo $this->Form->input('province');
echo $this->Form->input('price');
echo $this->Form->input('bedrooms');
echo $this->Form->input('bathrooms');
echo $this->Form->input('utilities', array('label' => 'Utilities Included'));
echo $this->Form->input('washer_dryer');
echo $this->Form->input('dishwasher');
echo $this->Form->input('a_c');
echo $this->Form->input('parking_spots');


echo $this->Form->hidden('image_files');
echo $this->Form->input('description');

if ($this->action == 'edit') {
    $buttonLabel = 'Save changes';
} else {
    $buttonLabel = 'Add house';
}

echo $this->Form->button($buttonLabel, array('id' => 'addButton'));
echo $this->Form->end();
  • 写回答

1条回答 默认 最新

  • doucongqian6644 2011-06-29 16:11
    关注

    You need to use the form helper and explicitly set the url the form points to.

    <?php echo $form->create('Post', array('url' => $html->url(array('controller'=>'posts', 'action'=>'edit', $this->data['Post']['id'])))); ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器