2010-12-21 69 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验证错误字符串形式呼应出来,以用于预填充表单的任何其他数据)