【问题标题】:Codeigniter form validation return always falseCodeigniter 表单验证总是返回 false
【发布时间】:2018-09-14 13:21:31
【问题描述】:

在我的 codeigniter 控制器函数调用 $this->form_validation->run() 中,它总是返回 false,而我的 validation_errors() 没有显示错误,可能是因为在 post 方法中没有接收到数据...

我的控制器

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

}

我的看法

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

【问题讨论】:

  • 你没有设置任何规则
  • 我已设置$this-&gt;form_validation-&gt;set_rules('selectUser','selectUser');
  • 我强烈建议阅读 CodeIgniters 非常全面的文档。它真的帮了我很多。链接:codeigniter.com/user_guide/general/welcome.html

标签: php html codeigniter


【解决方案1】:

为了使用 CodeIgniters 内置方法获取整个 $_POST 数组,您必须将第一个参数设置为NULL,将第二个参数设置为TRUE

像这样:

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

另外,您还没有设置任何验证规则..

在 CodeIgniter 中,您在 form_validation 对象内的 set_rules 方法的第三个参数中设置规则。

像这样:

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

您可以将第一个 $FIELD_NAME 替换为您要验证的 HTML 元素上的 name 属性值。

在向用户显示错误消息时,您可以将第二个 $FIELD_NAME 替换为您希望用于该字段的名称。

您可以将$RULES 替换为验证规则,例如:'required|min_length[#]|max_length[#]'

希望这会有所帮助!

【讨论】:

  • 但是如果我的$this-&gt;form_validation-&gt;run() return 总是 false 不进入 else
  • $this-&gt;form_validation-&gt;set_rules() 中需要 $RULES 吗?
【解决方案2】:

如果您没有设置规则(这使得使用$this-&gt;form_validation-&gt;set_rules() 变得毫无意义),表单验证将失败,因为它缺少必需的参数。

如果您不需要验证字段,请不要设置规则。

尝试将您的 set_rules 指令更新为 $this-&gt;form_validation-&gt;set_rules('selectUser','selectUser', 'required'); 以查看其行为是否正确。您可以通过在表单中​​填写某些内容(验证将通过)或将字段留空(验证将失败)来验证

请记住,如果您不会为某个字段设置至少一个验证规则,请不要为该字段实例化 set_rules 方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2016-10-28
    • 1970-01-01
    • 2012-08-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多