좀 도와주세요. 나는 전체 게시물을 가진 게시물 페이지와 코멘트를 추가하기위한 작은 양식을 게시합니다. 게시물 페이지의 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;
}
여기서'function validate()'는 어디에 있습니까? –
예,이 함수는 유효성 검사를 수행합니다. 그것은 MY_Model 안에 있습니다 : https://github.com/jamierumbelow/codeigniter-base-model/blob/master/core/MY_Model.php – Lykos
양식을 제출할 때, 그것은'if ($ this-> form_validation-> run() == true) {...}'문장? – anvoz