2011-06-29 5 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"> 

おかげ


EDIT:あなたはする必要があり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

'edit'メソッドコードを投稿できますか? – Ross

+0

投稿、ありがとう! – AlexBrand

答えて

1

: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フォームヘルパーを使用し、明示的にurを設定するlフォームはを指します。

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

私はこれを行い、それは動作しますが、ケーキはこれを自動的に行うべきではありませんか? – AlexBrand

+0

これは1.2の構文です。 1.3を使っているなら '$ this-> Form-> Create( 'Model')'でなければなりません。そして、はい、Cake *は自動的にこれを行う必要があります(モデル名のみ)。あなたは編集ビューで何を持っていますか? – Ross

+0

条件付きの$ this-> Form-> create( 'Post')という追加ビューをレンダリングします。アクションが編集の場合は、 'action' => 'editパラメーターが含まれます。それ以外の場合は$ this-> Form-> create( 'Post')だけをエコーし​​ます – AlexBrand

関連する問題