Codeigniter构建验证类在你自己的需要 [英] Codeigniter build validation class at your own needs

查看:240
本文介绍了Codeigniter构建验证类在你自己的需要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Codeigniter和Iam工作,希望使用Validation_form库和我的自定义规则来创建自己的自定义验证类,我将放置我自己的验证规则,并在我的项目中的任何地方使用,但这似乎不可能,我



Codeigniter kindle强迫我在我的控制器中做我的回调方法,但我需要他们在我的库或方法或任何地方其他!!!



我的问题是,我可以建立一个特定的库,我将放置我的验证规则和其他我需要处理的函数吗?


application / libriries 中创建一个新库,并将文件命名为

解决方案

MY_Form_validation



你在这里做的是扩展 form_validation 类,



MY _ 是您的配置上设置的,请务必

> class MY_Form_validation扩展CI_Form_validation
{
//这是类的强制性
//不要忘记这个或者它不会工作
public function __construct ($ rules = array(){
parent :: __ construct($ rules);
$ this-> CI-> lang-> load('MY_form_validation');
}
public function method1($ str){
return $ str ==''? FALSE:TRUE;
}
pulic函数method2($ str)
{
//如果你想从数据库验证
//你可以在这里加载
/ /或检查`system / libraries / form_validation`上的`form_validation`文件

}

public function check_something_with_post($ tr)
{
return $ this-> CI-> input-> post('some_post')== FALSE? FALSE:TRUE;
}
}

基本上,当调用规则示例 method1 | method2 您的post字段的值将是方法的参数。如果你想检查其他帖子你可以使用 $ this-> CI-> input-> post('post'的名称); / $>

当你想传递一个参数时,只需查看表单验证 is_unique unique 系统/ libraries / form_validation 上的代码。



错误消息,它会去应用程序/ language / english / MY_Form_validation_lang



示例 MY_form_validation_lang.php

  $ lang ['method1'] =错误错误错误。 
$ lang ['method2'] =这是一条错误消息。

如果 english code> application / language 只是创建它。



请在 创建库





在某些 linux debian 服务器,您可能需要将文件名从 MY_Form_validation 更改为
MY_form_validation f 上的表单


I am working in codeigniter and Iam looking to make my own custom validation class using "Validation_form" library and my custom rule where I will place my own validation rules and use that from everywhere in my project, but this seems impossible, I tried in couples ways to handle this but nothing.

Codeigniter kindle force me to make my callback methods in my controller but I need them in my library or "method" or wherever else!!!

My question is, can I build an specific library where I'll place my validation rules and other functions I need to handle that?

解决方案

you could create a new library in application/libriries and name the file MY_Form_validation

What you are doing here is extending the form_validation class so that you will not need to mess with the core files.

The MY_ is what is set on your config, be sure to check it if you changed yours.

sample MY_Form_validation.php

class MY_Form_validation Extends CI_Form_validation
{
   //this is mandatory for this class
   //do not forget this or it will not work
   public function __construct($rules = array(){
        parent::__construct($rules);
        $this->CI->lang->load('MY_form_validation');
    }
   public function method1($str){
     return $str == '' ? FALSE : TRUE;
   }
   pulic function method2($str)
   {
       //if you want a validation from database
       //you can load it here
       // or check the `form_validation` file on `system/libraries/form_validation`

  }

  public function check_something_with_post($tr)
  {
      return $this->CI->input->post('some_post') == FALSE ? FALSE : TRUE;
  }
}

Basically, when you call a rule sample method1|method2 the value of your post field will be the parameter of the method. if you want to check other post you can do it by using $this->CI->input->post('name of the post');

when you want to pass a parameter just look at the form validation is_unique or unique code on system/libraries/form_validation you will have an idea.

To create a error message that goes with it go to application/language/english/MY_Form_validation_lang

Sample MY_form_validation_lang.php

$lang['method1'] = "error error error.";
$lang['method2'] = "this is an error message.";

if english does not exist on your application/language just create it.

check more atCreating libraries

NOTE:

On some linux or debian server you may want to change the file name from MY_Form_validation to MY_form_validation note the small f on the word form.

这篇关于Codeigniter构建验证类在你自己的需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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