开发者

codeigniter validate

Hello I have a forum and when a user creates a comment, I want that if he didn't type anything I want to show him an error that he must type something in :) but I dont know how to put him the the thread he is in.

I have this

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();
}


You could do this with jquery but if that is not an option you could get the forum or topic id from the url (assuming your are using the url this way).

For example:

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

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

Or

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

Hope this helps.


Thanks for helping i can see what you mean but it just dont work :b,

i have this

$topic_id = $this->uri->segment(3); $this->post($topic_id); return;

and my url is

localhost:8888/ci/index.php/forum/create_comment

it looks like it cant find the ID

my URL to the forum is localhost:8888/ci/index.php/forum/post/33

this is my functions

function create_comment() { if($this->_submit_validate_comment() == false) { $id = $this->uri->segment(3); $this->post($id); return; //echo "validate fejl, kontakt lige en admin!"; } else { $data = array( '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';

$this->load->view('includes/template', $data); }


Why not pass in the return uri in the form submission using a hidden input field? No additional work will be needed by the controller other than validation of the return uri before performing a redirect.

Place the validation error string in session class's flashdata for echoing out in the form, along with any other data used to pre-populate your form)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜