2010-12-21 70 views
0

你好我有一個論壇,當用戶創建一個評論,我想,如果他沒有輸入任何我想告訴他一個錯誤,他必須輸入的東西:)但我不知道如何把他線程他在不在。codeigniter驗證

我有這個

if($this->_submit_validate_comment() == false) { 
    $this->post(); return; 
} 

function _submit_validate_comment() { 
    $this->form_validation->set_rules('kommentar', 'kommentar', 'required|min_length[4]'); 
    return $this->form_validation->run(); 
} 

回答

3

你可以用jQuery做到這一點,但如果這是不是一種選擇,你可以得到從URL論壇或主題ID(假設您正在使用這種方式的網址)。

例如:

http://yoursite.com/forum/topic/12

if($this->_submit_validate_comment() == false) 
{ 
    $topic_id = $this->uri->segment(3); 
    redirect('/forum/topic/'. $topic_id); 
} 

或者

if($this->_submit_validate_comment() == false) 
{ 
    $topic_id = $this->uri->segment(3); 
    $this->topic($topic_id); 
} 

希望這有助於。

+0

在您的重定向方法,則需要使用/論壇/後/」一起。 $ this-> input-> post('id'),你的基地址是localhost:8888/ci /。這可能是一個URL的問題。嘗試添加base_url()。 '/ forum/post /'....到你的重定向,看看是否有幫助。我可能是錯的,但嘗試將uri段從3改爲4,以防萬一,或者更好地回顯$ this-> uri-> segment(3)的值,以確保獲得正確的值。 – 2010-12-21 19:02:03

0

感謝您的幫助,我可以明白你的意思,但它只是不工作:B,

我有這個

$ topic_id = $這個 - > URI->段(3); $ this-> post($ topic_id); return;

和我的網址是

本地主機:8888/CI/index.php文件/論壇/ create_comment

它看起來像它無法找到該ID

我的網址,以論壇爲 本地主機: 8888/CI/index.php文件/論壇/後/ 33

這是我的職能

函數create_comment(){ if($ this - > _ submit_validate_comment() == false){$ id = $ this-> uri-> segment(3); $ this-> post($ id);返回; // echo「validate fejl,kontakt lige en admin!」; } else {$ data = array this''fk_forum_traad'=> $ this-> input-> post('id'), 'brugernavn'=> $ this-> session-> userdata('username'), 'indhold'=> $ this-> input-> post('kommentar'), 'dato'=>'fejl');

$ this-> load-> model('forum_model'); $ this-> forum_model-> create_comment($ data); redirect('/ forum/post /'。 $ this-> input-> post('id')。'', 'refresh'); } }

function post($ id){ $ this-> load-> model('forum_model'); $ data ['query'] = $ this-> forum_model-> posts($ this-> uri-> segment(3));

$ this-> load-> model('forum_model'); $ data ['comments'] = $ this-> forum_model-> comments($ this-> uri-> segment(3));

$data['content'] = 'forum_post_view'; 

$這 - >負載>視圖( '包括/模板', $數據); }

0

爲什麼不通過使用隱藏的輸入字段的表單提交返回uri?在執行重定向之前,除了驗證返回uri之外,控制器不需要額外的工作。

將在會議類的flashdata驗證錯誤字符串形式呼應出來,以用於預填充表單的任何其他數據)