开发者

Form action loses post ID after validation error - 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();


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'])))); ?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜