2011-06-29 82 views
1

如果我沒有任何驗證錯誤,我的編輯操作非常有效。驗證錯誤後,表單操作會丟失帖子ID - CakePHP

當發現驗證錯誤時,視圖會再次顯示錯誤消息,但表單標記操作中的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:add.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

你可以發佈你的編輯方法代碼嗎? – Ross

+0

已發佈,謝謝! – AlexBrand

回答

1

您需要使用表單助手並明確設置你的l表格指向。

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

我做到了這一點,它的工作原理..但不應該蛋糕做這個automagically? – AlexBrand

+0

這是1.2語法;如果你使用1.3,它應該是'$ this-> Form-> Create('Model')',否則它是正確的。是的,Cake *應該*自動執行此操作(只是模型名稱),您在編輯視圖中有什麼? – Ross

+0

我渲染添加視圖,它有一個條件$ this-> Form-> create('Post')。如果動作是編輯,則包括'動作'=>'編輯參數。否則,它只會回顯$ this-> Form-> create('Post') – AlexBrand