2013-10-28 3 views
0

좀 도와주세요. 나는 전체 게시물을 가진 게시물 페이지와 코멘트를 추가하기위한 작은 양식을 게시합니다. 게시물 페이지의 URI는 site/posts/1이므로 게시물 컨트롤러에 있고 양식 작업은 form_open(site_url('comments/add/'.$post->post_id))입니다. CodeIgniter - 게시물에 댓글 달기

는 의견 컨트롤러 안에 내 추가() 함수입니다 :

public function add($post_id){ 
    // if nothing posted redirect 
    if (!$this->input->post()) { 
     redirect(site_url()); 
    } 

    // TODO: save comment in database 
    $result = $this->comment_model->add($post_id); 
    if ($result !== false) { 
     redirect('posts/'.$post_id); 
    } 

    // TODO:load the view if required 
} 

이것은

public function add($post_id){ 
    $post_data = array(
     'post_id' => $post_id, 
     'username' => $this->input->post('username'), 
     'email'  => $this->input->post('email'), 
     'comment' => $this->input->post('comment') 
    ); 

    if ($this->validate($post_data)) { 

     $this->db->insert('comments', $post_data); 

     if ($this->db->affected_rows()) { 
      return $this->db->insert_id(); 
     } 
     return false; 
    } else { 
     return false; 
    } 
} 

가 난 할 노력하고있어하는 경우 인 코멘트 모델 내에서 추가() 함수 $ result = $ this-> comment_model-> add ($ post_id); 유효성 검사 오류가 내 게시물보기에 표시되지 않습니다. 그렇지 않으면 주석을 삽입하고 같은 게시물 페이지 (site/posts/1)로 리디렉션합니다.

문제는 내가 제출을 누르면 양식 동작이 주석/추가/1을 예상대로 수행하지만 위의 작업을 수행하지 않는다는 것입니다.

어떤 생각을 어떻게 해결할 수 있습니까 ??

편집 '혼동스러운'validate() 함수없이 코드를 약간 변경했습니다. 아마 이것은 더 도움이 될 것입니다.

코멘트 컨트롤러 :

public function add($post_id){ 
    // if nothing posted redirect 
    if (!$this->input->post()) { 
     redirect(site_url()); 
    } 

    // TODO: save comment in database 
    $this->form_validation->set_rules($this->comment_model->rules); 
    if ($this->form_validation->run() == true) { 
    echo "Ok! TODO save the comment."; 
    // $this->comment_model->add($post_id); 
    // redirect('posts/'.$post_id); 
    } else { 
     echo "Validation Failed! TODO: show validation errors!"; 
    } 

    // TODO:load the view if required 
} 

코멘트 모델 :

public function add($post_id){ 
    $post_data = array(
     'post_id' => $post_id, 
     'username' => $this->input->post('username'), 
     'email'  => $this->input->post('email'), 
     'comment' => $this->input->post('comment') 
    ); 

     $this->db->insert('comments', $post_data); 

     if ($this->db->affected_rows()) { 
      return $this->db->insert_id(); 
     } 
     return false; 
} 
+0

여기서'function validate()'는 어디에 있습니까? –

+0

예,이 함수는 유효성 검사를 수행합니다. 그것은 MY_Model 안에 있습니다 : https://github.com/jamierumbelow/codeigniter-base-model/blob/master/core/MY_Model.php – Lykos

+0

양식을 제출할 때, 그것은'if ($ this-> form_validation-> run() == true) {...}'문장? – anvoz

답변

0

당신은 멀리 Posts 컨트롤러에 다시 validation_errors()를 전달해야합니다. 잠시 후 add 함수에서 리디렉션을 수행하면 (유효성 검사가 실패한 경우) 유효성 검사 오류가 누출됩니다. 내가 다시 Posts 컨트롤러로 Comments 컨트롤러에서 성공/오류 메시지를 전달하는 데 flashdata (http://ellislab.com/codeigniter/user-guide/libraries/sessions.html) 사용을 고려할 것

. 아래에 비슷한 :

댓글 컨트롤러 :

public function add($post_id) { 
    // if nothing posted redirect 
    if (!$this->input->post()) { 
     redirect(site_url()); 
    } 

    // TODO: save comment in database 
    $this->form_validation->set_rules($this->comment_model->rules); 
    if ($this->form_validation->run() == true) { 
     // Store the success message in flash data 
     $this->session->set_flashdata('message', 'Ok! TODO save the comment.'); 
     // Redirect back to posts page 
     redirect('posts/'.$post_id, 'refresh'); 
    } else { 
     // Store the error message in flash data 
     $this->session->set_flashdata('message', validation_errors()); 
     // Redirect back to posts page 
     redirect('posts/'.$post_id, 'refresh'); 
    } 
} 

게시물 컨트롤러 :

public function index($post_id) { 
    $this->data['message'] = $this->session->flashdata('message'); 
    $this->load->view('posts', $this->data); 
} 

게시물보기 :

echo $message; 

완벽하지는 않지만 도움이되기를 바랍니다 ...

+0

사실!리디렉션을 사용하면 validation_errors()가 손실되므로 세션 플래시 데이터에 저장하고 다시 가져 오는 것이 가장 좋은 방법이라고 생각합니다. 그러나 다른 더 좋은 방법이 있다면 당신이나 다른 누군가가 그것을 게시하면 나는 감사 할 것입니다. 감사! – Lykos

+0

유효성 검사가 실패하면 어떻게 각 필드의 게시 값을 유지할 수 있습니까? 나 또한 (유효성 검사 오류로) 다음과 같이 할 수도있다. $ this-> session-> flashdata ('post_values', $ this-> input-> post()) ?? – Lykos

+0

예. 바로 그거야 !! 그렇게하면 발생한 모든 오류와 POST 된 데이터를 다시 전달하게됩니다. 다시 .. 완전하지 않을지도 모르지만 직업을 !! –

관련 문제