在CodeIgniter(PHP)中从库设置form_validation规则

ui7jx7zq  于 2022-12-06  发布在  PHP
关注(0)|答案(1)|浏览(115)

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.
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.
My problem is :

  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. 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.
    It's 6 hours i'm looking for a solution and there's absolutely nothing clean :
  • 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 :(
  • 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'); // Then you can call any $this->lbl_validation->method() with $this->method() in the controller

  • 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.
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.
Maybe, there's someone with a good dans clean solution. All my hope are turned to the StackOverFlow community ; someone ? A crazy CI geek ?
Thank you ;)

ars1skjm

ars1skjm1#

处理验证的唯一好的方法是在保存到数据库之前把验证规则放在最后的手段,换句话说,在模型中。这样做,同样的规则可以在任何控制器或库中使用,而不需要重新定义。
下面的想法摘自Jamie Rumbelows优秀的Codeigniter手册:
只需在模型中创建一个数组:

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

然后实现一个方法,您可以使用该方法在保存()之前验证数据

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

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

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

相关问题