从CodeIgniter(PHP)中的库设置form_validation规则 [英] Set a form_validation rule from a library in CodeIgniter (PHP)

查看:142
本文介绍了从CodeIgniter(PHP)中的库设置form_validation规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我在 CodeIgniter灵活性上遇到了一个大问题。我有很多针对个人的规则(例如20条规则),针对的是大型项目中表单的输入。

Ok so i've got a big problem with CodeIgniter flexibility. I've many personal rules (like 20) targeting an input in a form within a huge project.

在使用CodeIgniter的经典 callback_系统时,所有工作都可以进行。我只是将这些方法放在与表单检查相同的类中,并且可以正确地对其进行检查。

Everything work while I use the classic "callback_" system by CodeIgniter. I just put the methods in the same class than the form check and it checks it correctly.

我的问题是:

1)我想在另一个控制器中的另一个form_validation中使用所有这些规则,而无需复制/粘贴代码; 我们都知道它很脏/很邪恶

1) I'd like to use all these rules in another form_validation in another controller without copy/paste the code ; we all know it's dirty/evil.

2)理想情况下,我希望将这些规则放在一个大库中,因为这需要大约800线的东西,把它放在我的控制器里不是一个好主意;正如我说的那样,这个项目相当庞大。

2) Ideally, i'd appreciate to put these rules in a big library, because it takes something like 800 lines and this is not a good idea to let it be in my controller ; as I said this project is quite huge.

这是我要寻找解决方案的6个小时,而且绝对没有任何问题:

It's 6 hours i'm looking for a solution and there's absolutely nothing clean :


  • 我已经有了 MY_Form_Validation 来提出一些一般规则,但我不喜欢将其混用全局类中的特定规则,每次都会徒劳地调用它。加上这些规则包含许多库,模型,本机CI核心方法(例如$ this-> input-> post()),当我将所有内容放入此MY_Form_Validation时,它们会生成错误。不是一个好的解决方案:(

  • I already have a MY_Form_Validation to put some general rules but i don't like the idea to mix my specific rules in a global class which will call it everytime vainly. Plus these rules contain many libraries, models, native CI core methods such as $this->input->post() which generate errors when I put everything in this MY_Form_Validation. Not the good solution :(

我创建了一个 MY_Controller ,其中包含一个名为 imports的方法,该方法可在其中重新生成选定的库方法控制器(在PHP4中,如果人们好奇,它就是一种 aggregate_methods函数);系统运行良好,但CodeIgniter无法理解它。方法可以在控制器内调用,但似乎框架检查$ CI内容调用规则( / system /中的Form_validation.php第590行),以至于最后它不起作用;修改此核心部分也很困难,我宁愿不动手就放弃。

I created a MY_Controller including a method named 'imports' which re-generate selected libraries methods within the controller (in PHP4 it was kind of the 'aggregate_methods' function if people are curious) ; the system works perfectly but CodeIgniter doesn't understand it. The methods can be called within the controller but it seems the framework check the $CI content to call the rules (Form_validation.php line 590 in '/system/') so it doesn't work at the end ; it's also hard to modify this core part, I prefered not touching it and gave up.


$ this-> load-> library('volt / lbl_validation');
$ this-> imports('Lbl_validation');
//然后您可以在控制器中使用$ this-> method()调用任何$ this-> lbl_validation-> method()

$this->load->library('volt/lbl_validation'); $this->imports('Lbl_validation'); // Then you can call any $this->lbl_validation->method() with $this->method() in the controller




  • 我试图破解CI在我的库中创建一个自定义的form_validation('lbl_validation'); t他的系统有点混乱,但是有效。问题是当我回到CI form_validation系统以显示错误消息时,这是一个真正的意大利面条代码,效果不佳。 也不是一个好的解决方案

    • I tried to hack CI creating a customized form_validation within my library ('lbl_validation') ; the system was a bit messy but worked. The problem is when i came back to the CI form_validation system to show error messages, it was a true spaghetti-code which wasn't working that well. Not the good solution either.
    • 我也尝试了其他一些糟糕的解决方案,但我不想承认。

      I also tried some other shitty solutions but i prefer not confess it.

      现在我在电脑前问自己好人为什么会遇到坏事,为什么这么难分开set_rules从CodeIgniter中的被调用方法中,为什么他们不提前计划,人们可能需要将库方法作为规则来调用。我不知道该怎么办,我很犹豫将笨拙的require()放在某个地方,使其像现在的办公桌一样变得肮脏和凌乱。

      Now i'm here in front of my computer asking myself why bad things happened to good people, why this is so hard to separate set_rules from the called methods in CodeIgniter, why they didn't plan ahead people could've needed to call libraries methods as rules. I don't know what to do and i'm hesitating to put a dumb require() somewhere and make it all dirty and messy like my desk right now.

      也许,有些人的Dans Clean解决方案很好。我所有的希望都转向了StackOverFlow社区;有人吗?疯狂的CI怪胎?

      Maybe, there's someone with a good dans clean solution. All my hope are turned to the StackOverFlow community ; someone ? A crazy CI geek ?

      谢谢;)

      推荐答案

      唯一有效的DRY处理验证方法是在保存到数据库(即在模型中)之前将验证规则放在最后的手段。这样,可以在任何控制器或库中使用相同的规则而无需重新定义。

      The only good, DRY way to handle validation is to put validation rules at the last resort before saving to the the database, in other words in the models. By doing this, the same rules can be used in any controller or library without being redefined.

      以下想法摘自Jamie Rumbelows出色的Codeigniter手册:

      The following idea is taken from Jamie Rumbelows excellent Codeigniter handbook:

      只需在模型中创建一个数组:

      Simply create an array in your model:

      $validate = array(
          array( 'field' => 'username', 'label' => 'Username', 'rules' => 'required,trim' ),
          array( 'field' => 'password', 'label' => 'Password', 'rules' => 'required|min_length[8]' )
      );
      

      然后在save()之前实现一种可用于验证数据的方法

      Then implement a method that you can use to validate your data prior to save()

      function validate($data) {
          if (!empty($this->validate)) {
              foreach ($data as $key => $value) {
                 $_POST[$key] = $value; }
                 $this->load->library('form_validation');
                 $this->form_validation->set_rules($this->validate);
                 return $this->form_validation->run(); }
          else
          {
              return TRUE;
          } 
      }
      

      现在,在控制器中,您可以使用:

      Now, in your controllers you can use:

      if ($this->user->validate($user))
          save...
      

      这篇关于从CodeIgniter(PHP)中的库设置form_validation规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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