【问题标题】:Form action loses post ID after validation error - CakePHP验证错误后表单操作丢失帖子 ID - CakePHP
【发布时间】:2011-06-29 15:12:49
【问题描述】:

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

当发现验证错误时,视图会再次呈现错误消息,但表单标签操作中的 URL 会丢失帖子 ID。

例如

验证错误之前:

<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 中添加了“编辑”方法

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

【问题讨论】:

  • 您可以发布您的edit 方法代码吗?

标签: php html forms cakephp validation


【解决方案1】:

您需要使用表单助手并明确设置表单指向的 url。

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

【讨论】:

  • 我做了这个并且它有效..但是蛋糕不应该自动做这个吗?
  • 那是 1.2 的语法;如果您使用的是 1.3,则应该是 $this-&gt;Form-&gt;Create('Model'),否则是正确的。是的,Cake 应该自动执行此操作(只是模型名称),您的编辑视图中有什么?
  • 我渲染了添加视图,它有一个条件 $this->Form->create('Post')。如果动作是编辑,它包括 'action' => 'edit 参数。否则,它只会回显 $this->Form->create('Post')
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-24
  • 2011-07-08
  • 2016-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多