如何在codeigniter中的自定义库中设置规则表单验证

时间:2017-08-29 10:00:11

标签: forms codeigniter set rules

//我试图用输入加载数组并检查输入然后保存数据

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Slider{

      public $ci;
       public $ci2;
        protected $CI;
     function __construct() {
                 // reference to the CodeIgniter super object 
         $this->CI =& get_instance();
             $this->ci =&get_instance();
           $this->ci->load->model('Slider_Model');
                   $this->CI->load->library('form_validation');
    }

//使用输入加载数组并检查输入,然后保存数据enter image description here

       function AddSlide($Data){
$this->CI->load->library('form_validation');
$this->CI->form_validation->set_error_delimiters('<p style="color: red">', '</p>');
$this->CI->form_validation->set_rules($Data['Title'],$Data['Title'],'min_length[5]|max_length[20]'); 
$this->CI->form_validation->set_rules($Data['Description'],$Data['Description'],'min_length[10]|max_length[100]'); 
$this->CI->form_validation->set_rules($Data['Status'],$Data['Status'],'in_list[1,2]');

if ($this->CI->form_validation->run() == FALSE)
{
   echo FALSE;
}
else
{
   $this->ci->Slider_Model->AddNewSlid($Data);
}

}











}

1 个答案:

答案 0 :(得分:0)

请检查您的form_validation库是否已加载

您应该需要更改此条件

if ($this->CI->form_validation->run() === FALSE) 

到此if ($this->CI->form_validation->run() == FALSE)

function AddSlide($Data)
{ 
$this->load->library('form_validation');

$this->form_validation->set_error_delimiters('<p style="color: red">', '</p>');
$this->CI->form_validation->set_rules($Data['Title'],'sasass','min_length[5]|max_length[20]'); 
$this->CI->form_validation->set_rules($Data['decription'],$Data['decription'],'min_length[10]|max_length[100]'); 
$this->CI->form_validation->set_rules($Data['status'],$Data['status'],'in_list[1,2]');

if ($this->CI->form_validation->run() == FALSE)
{
   echo FALSE;
}
else
{
   $this->ci->Slider_Model->AddNewSlid($Data);
}

}