dszpyf4859 2018-09-14 13:21
浏览 55
已采纳

Codeigniter表单验证返回始终为false

In my codeigniter controller function call $this->form_validation->run(), that return always false, and my validation_errors() not showing error, probably because not receive datas in post method...

my controller

class Reminder extends CI_Controller {
public function __construct()
{
    parent::__construct();
    $this->load->model('reminder_model');
    $this->load->helper('form');
    $this->load->library('form_validation');
    $this->load->helper('url');
    $this->load->library('email');
    $this->load->library('session');

    if(!$this->session->auth_ok) {
        redirect('auth/login');        
    }
}

public function index(){
    $data['title'] = 'Reminder';
    $data['section'] = 'reminder';

    $data['reminders'] = $this->reminder_model->getReminders();

    $data['operatori'] = $this->reminder_model->getOperators();

    $this->form_validation->set_rules('selectUser','selectUser', '');

    if($this->form_validation->run() === FALSE) {
        $this->load->view('common/header2', $data);
        $this->load->view('reminder/index', $data);
        $this->load->view('common/footerReminder');

        echo validation_errors();
    }else{
        echo "<pre>";
        print_r($this->input->post());
        die();
    }

}

my view

<?php echo form_open('reminder/index'); ?>

<div class="form-group">
    <label for="selectUser" style=" width: 30%">Utente: </label>
    <select class="form-control" name="selectUser" id="selectUser" style="width: 30%">
        <?php foreach($operatori as $operatore): ?>
            <option value="<?php echo $operatore['ID']?>" <?php echo $r = ($operatore['ID']==$this->session->auth_user['ID']) ? 'selected' : '' ?>><?php echo $operatore['nome']." ".$operatore['cognome'] ?></option>
        <?php endforeach; ?>
    </select>
</div>


<button type="submit" class="btn btn-primary"><i class="fas fa-search"></i> View</button>

<?php echo form_close(); ?>
  • 写回答

2条回答 默认 最新

  • douwen3198 2018-09-14 13:30
    关注

    In order to get the entire $_POST array using CodeIgniters built-in methods, you have to set the first parameter as NULL and the second parameter as TRUE

    Like this:

    $this->input->post(NULL, TRUE);
    

    Also, you have not set any rules for validation..

    In CodeIgniter, you set rules in the third parameter of the set_rules method within the form_validation object.

    Like this:

    $this->form_validation->set_rules($FIELD_NAME, $FIELD_NAME(for error messages), $RULES);
    

    You would substitute the first $FIELD_NAME with the value of the name attribute on the HTML element you are looking to validate.

    You would substitute the second $FIELD_NAME with the name you would like to use for the field when displaying an error message to the user.

    You would substitute $RULES with the validation rules such as: 'required|min_length[#]|max_length[#]'

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献