Codeignitervalidation_errors()总是返回空 [英] Codeigniter validation_errors() always returning empty

查看:117
本文介绍了Codeignitervalidation_errors()总是返回空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的表单,其中添加了以下验证规则。 表单位于autoload.php中。



每当任何验证规则被破坏时,页面均不会显示错误输出,而当任何验证规则都没有破坏时,表单将按预期进行。



这是我的控制器函数

 公共函数add()
{

$ this-> load-> library('form_validation');
$ this-> load-> library('layout');
//字段名称,错误消息,验证规则
$ this-> form_validation-> set_rules('type','Type','required | xss_clean');
$ this-> form_validation-> set_rules('title_type','Title type','required | xss_clean');
$ this-> form_validation-> set_rules( first_name, First name, trim | required | min_length [1] | max_length [150] | xss_clean);
$ this-> form_validation-> set_rules(姓氏,姓氏,修剪|必需| min_length [1] | max_length [150] | xss_clean);
$ this-> form_validation-> set_rules('job_title','Job title','trim | min_length [5] | max_length [300] required | xss_clean');
$ this-> form_validation-> set_rules('office_number','Office number','trim | min_length [2] | max_length [11] | xss_clean');
$ this-> form_validation-> set_rules(电话,电话, trim |数字| xss_clean);
$ this-> form_validation-> set_rules('email','Email','trim | valid_email | xss_clean');
$ this-> form_validation-> set_rules(网站,网站,修剪| prep_url | xss_clean);
$ this-> form_validation-> set_rules(背景,背景,修剪| xss_clean);


$ data = array(
‘title’=>‘Add staff member’
);

if($ this-> form_validation-> run()== FALSE){//错误

$ this-> session-> set_flashdata( '消息','您错过了一些详细信息,请重试。')

$ this-> layout-> load(默认, add_person,$ data);
} else {
$ this-> people_model-> add_person();
$ this-> layout-> load(默认值, addended_person,$ data);
}


} //添加

在我的表单视图中,我有

 <?php echo validate_errors('< p class = msg msg-error >')吗? 

谁能看到我做错了什么,我已经尽力了。 / p>

validation_errors()的var_dump给出

  string''(长度= 0)

编辑:奇怪的是,如果我将这两行交换,我会得到:

  $ this-> load-> library('form_validation'); 
$ this-> load-> library('layout'); // lib代码http://pastebin.com/K1rBV512

消息:未定义的属性:People :: $ form_validation

文件名:controllers / people.php

行号:27


解决方案

$ data 数组传递给模型。所有输入都需要从 VIEW 页面获得 POST 。您没有显示任何特定的错误,因此很难诊断出您的问题。



请确保所有 POST 在您的 MODEL 中输入以下内容:

 `'first_name'=> ; $ this-> input-> post('first_name'),`



在您的视图中

 <?php echo validate_errors ('< p class = msg msg-error>')吗? 

在您的视图中将其更改为:

 < p class = msg msg-error><?php echo validate_errors(); && lt; / p> 


I have a simple form adding to my db with the below validation rules. "form" is in autoload.php.

Whenever any of the validation rules are broken the page shows no error output, when none are broken the form goes through as expected.

here's my controller function

  public function add()
{

    $this->load->library('form_validation');
    $this->load->library('layout');
    // field name, error message, validation rules
    $this->form_validation->set_rules('type', 'Type', 'required|xss_clean');
    $this->form_validation->set_rules('title_type', 'Title type', 'required|xss_clean');
    $this->form_validation->set_rules('first_name', 'First name', 'trim|required|min_length[1]|max_length[150]|xss_clean');
    $this->form_validation->set_rules('surname', 'Surname', 'trim|required|min_length[1]|max_length[150]|xss_clean');
    $this->form_validation->set_rules('job_title', 'Job title', 'trim|min_length[5]|max_length[300]required|xss_clean');
    $this->form_validation->set_rules('office_number', 'Office number', 'trim|min_length[2]|max_length[11]|xss_clean');
    $this->form_validation->set_rules('telephone', 'Telephone', 'trim|numeric|xss_clean');
    $this->form_validation->set_rules('email', 'Email', 'trim|valid_email|xss_clean');
    $this->form_validation->set_rules('website', 'Website', 'trim|prep_url|xss_clean');
    $this->form_validation->set_rules('background', 'Background', 'trim|xss_clean');


    $data = array(
        'title' => 'Add staff member'
    );

    if ($this->form_validation->run() == FALSE) { //Was errors

        $this->session->set_flashdata('message','You missed some details, please try again.');

        $this->layout->load('default', 'add_person', $data);
    } else {
        $this->people_model->add_person();
        $this->layout->load('default', 'added_person', $data);
    }


} //add

And in my form view i have

    <?php echo validation_errors('<p class="msg msg-error">') ?>

Can anyone see what I've done wrong, I've tried everything I can think of.

A var_dump of the validation_errors() gives

string '' (length=0)

edit: strangely, if i swap these two lines around, i get:

    $this->load->library('form_validation');
    $this->load->library('layout'); //lib code http://pastebin.com/K1rBV512

Message: Undefined property: People::$form_validation

Filename: controllers/people.php

Line Number: 27

解决方案

You need to pass the $data array to your model. All the inputs need to get POST from the VIEW page. You're not showing any specific errors so it's hard to diagnose your problem.

Make sure you POST all the inputs like this in your MODEL:

`'first_name' => $this->input->post('first_name'),`


In your view:

<?php echo validation_errors('<p class="msg msg-error">') ?>

In your view change it to this:

<p class="msg msg-error"><?php echo validation_errors(); ?></p>

这篇关于Codeignitervalidation_errors()总是返回空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆