dszpyf4859 2018-09-14 05: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 05: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部