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

时间:2011-06-29 15:12:49

标签: php html forms cakephp validation

如果我没有任何验证错误,我的编辑操作会很有效。

如果找到验证错误,则会再次使用错误消息呈现视图,但是表单标记的操作中的URL会丢失帖子ID。

e.g。

验证错误之前:

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

验证错误后:

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

导致这种情况的原因是什么?

由于


编辑:在posts_controller.php中添加了'edit'方法

    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');

编辑2:添加.ctp视图

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 个答案:

答案 0 :(得分:1)

您需要使用表单助手并显式设置表单指向的URL。

<?php echo $form->create('Post', array('url' => $html->url(array('controller'=>'posts', 'action'=>'edit', $this->data['Post']['id'])))); ?>