【问题标题】:CodeIgniter - adding comments to a postCodeIgniter - 在帖子中添加评论
【发布时间】:2013-10-28 10:22:47
【问题描述】:

我需要一些帮助。我有一个包含完整帖子的帖子页面,在帖子下方有一个用于添加 cmets 的小表格。 post页面的uri是:site/posts/1,所以在posts控制器里,form action是form_open(site_url('comments/add/'.$post->post_id))

这是我在 cmets 控制器中的 add() 函数:

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
}

这是注释模型中的 add() 函数

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);验证失败以在我的帖子视图中显示验证错误,否则插入评论并重定向到同一帖子页面(站点/帖子/1)。

问题是,当我点击提交时,表单操作按预期进入 cmets/add/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 里面见:github.com/jamierumbelow/codeigniter-base-model/blob/master/…
  • 当您提交表单时,它是否进入了if ($this->form_validation->run() == true) { ... } 语句?
  • 是的,如果我提交正确的表单,它会进入 if 语句,否则会进入 else .. 问题是我无法弄清楚如何在帖子上显示验证错误页面(在我的示例中:site/posts/1)

标签: php codeigniter codeigniter-2 codeigniter-url


【解决方案1】:

您不需要将validation_errors() 传递回您的Posts 控制器。此时,当您在 add 函数中执行重定向时(当验证失败时),您会丢失抛出的验证错误。

我会考虑使用flashdata (http://ellislab.com/codeigniter/user-guide/libraries/sessions.html) 将成功/错误消息从您的Comments 控制器传回您的Posts 控制器。类似于以下内容:

评论控制器:

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;

可能并不完美,但希望对您有所帮助...

【讨论】:

  • 真的!通过重定向,validation_errors() 会丢失,所以我想最好的方法是将它们存储在会话 flashdata 中并取回它们。但是,如果有其他更好的方法,如果您或其他人发布它,我将不胜感激。谢谢!
  • 如果验证失败,我怎样才能保留每个字段的帖子值?我也可以申请(也有验证错误)这样的东西: $this->session->flashdata('post_values', $this->input->post())??
  • 是的。就是这样!!这样,您将传回遇到的任何错误以及 POST 数据。再次......可能并不完美,但可以完成工作!
  • 是的,但我可以在 flashdata 中存储验证错误和发布值吗?我不确定我是否可以存储太多东西
  • 是的。您可以将两者都存储在 flashdata 中,如果您在 flashdata 中存储相当少量的内容,这将非常有效。默认情况下,flashdata 与 cookie 一起使用,并且 cookie 的大小有限。如果您要存储的数据量超过此限制,那么防止这种情况的简单方法是使用基于数据库的会话,您可以通过 config.php 文件中的以下项目启用它: $config['sess_use_database'] =真的;
猜你喜欢
  • 2020-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-06
  • 2013-12-02
  • 1970-01-01
相关资源
最近更新 更多