duanhunlou7051 2012-04-02 20:35
浏览 52
已采纳

form_validation有什么问题?

Helo,

So I have a little problem with form_validation in Codeigniter, here is the controller -

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Records extends CI_Controller {

 public function index()
 {
   $this->load->helper(array('form', 'url'));
   $this->load->library('form_validation');  
    $this->load->helper('url');   
    $this->load->view('includes/header'); 
  $this->load->view('products');
  $this->load->view('includes/footer');
 }

 public function addRecord() {  
    $this->load->helper(array('form', 'url'));  
    $this->load->helper('url');    
    $query = $this->db->get_where('users', array('uname' => $this->session->userdata('username')));
    foreach($query->result_array() as $row) {
      $data = array(
        'username' => $row['uname'],
        'email' => $row['email'],
        'name' => $row['name'],
        'description' => $row['description'],
        'picture' => $row['picture'],
        'gender' => $row['gender'],
      );      
    }    
    if($this->session->userdata('logged_in')) {
      $this->load->view('includes/header'); 
      $this->load->view('my_records', $data);
      $this->load->view('includes/footer');
    }
    else {
      redirect('/home', 'refresh');
    }    
  }

  public function postRecord() {
    $this->lang->load('form_validation', 'latvian');
   $this->load->library('form_validation');    
    $this->load->model('records');
    $this->load->helper(array('form', 'url'));    
    if(!$this->session->userdata('logged_in')) redirect('/home', 'refresh');                

    $this->form_validation->set_rules('message', 'Ziņa', 'required|trim|min_length[5]|max_length[250]');

    $query = $this->db->get_where('users', array('uname' => $this->session->userdata('username')));    
    foreach($query->result_array() as $row) {
      $data = array(
        'username' => $row['uname'],
        'email' => $row['email'],
        'name' => $row['name'],
        'description' => $row['description'],
        'picture' => $row['picture'],
        'gender' => $row['gender'],
      );      
    }  
    // If doesn't pass validation, redirect's back with errors.
  if ($this->form_validation->run() == FALSE)
  {                                   
      $this->load->helper('url');  
      $this->load->view('includes/header'); 
    $this->load->view('my_records', $data);
    $this->load->view('includes/footer');
  }
  // If everything is correct add's user.
  else
  {
      $this->load->view('includes/header');   
      $this->load->view('my_records', $data);
    $this->load->view('includes/footer');       
  }  
  }

} ?> 

and view -

<div class="list-products">
       <div>
         <?php if(validation_errors()) { ?><div class="error-submit"><p><?php echo validation_errors(); ?></p></div><?php } ?>       
              <form class="fix-this form" method="post" action="/savieno/records/postRecord">
          <div class="formfield">
            <span class="profile">Your message:</span>
            <textarea id="message" name="message" class="with-label" rows="10" cols="10" placeholder="your message."></textarea>
          </div>
          <div class="formfield">
            <input type="submit" id="submit" name="postRecord" value="Pievienot" class="fix-this" />
          </div>
              </form>
            </div> 

At start, it gave me error, that can’t find functio set_message(), but I added form_validation before form helper, and now it works, but again there is problem, cause it doesn’t display form error codes. It just ain’t displaying it at all, I have set error message for it, and it should work, but it isn’t. What could be the problem?

  • 写回答

1条回答 默认 最新

  • dourong4031 2012-04-03 03:04
    关注

    In your view, you don't need:

    <?php if(validation_errors()) { ?><div class="error-submit"><p><?php echo validation_errors(); ?></p></div><?php } ?>
    

    You can use just:

    <?php echo validation_errors('<div class="error-submit">', '</div>'); ?>
    

    No need for the if statement there. From glancing over this, that might be able to solve your problem.

    As a side note, in your Controller it's not necessary to have closing PHP tag, and it's actually considered best practice to leave it unclosed.

    Oh, and you could if you wanted also make a construct for:

    function __construct() {
        parent::__construct();
        $query = $this->db->get_where('users', array('uname' => $this->session->userdata('username')));
        foreach($query->result_array() as $row) {
    $data = array(
                'username' => $row['uname'],
                'email' => $row['email'],
            'name' => $row['name'],
            'description' => $row['description'],
                'picture' => $row['picture'],
            'gender' => $row['gender'],
            );      
    } 
    }
    

    Although, I think it would be better (MVC architecture) to just make that query available in a model (something) like this instead:

     $this->load->model('dude');
     $result = $this->dude->get_dude_where('$this->session->userdata('username'));
     foreach($result as $row) {
         $data = $result['result_array'];
     }
    

    But that would require you to setup a model function to pass/return $query->result_array().

    Anyways, I hope this resolved your form_validation problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮