【问题标题】:CodeIgniter creating multiple custom Form_validation extension classesCodeIgniter 创建多个自定义 Form_validation 扩展类
【发布时间】:2016-09-03 11:24:49
【问题描述】:

我知道我可以根据默认制作一个名为MY_Form_validation的文件

$config['subclass_prefix'] = 'MY_';

但是,如果我想用不同的MY_Form_validation 类表示不同的表单怎么办?如果可以,我将如何在验证控制器中加载和调用它?

例如MY_Student_registration_form_validationMY_Student_cancellation_form_validation等等?

网上的大部分问题都直截了当,建议创建这个唯一的类,但不要针对多个实例。

注意我尝试在 application/libraries (CI 3.x) 中创建 MY_Student_registration_form_validation 类并将其加载到我的控制器中使用

$this->load->library('my_student_registration_form_validation');

我收到了错误Non-existent class: My_student_registration_form_validation

然后我尝试不带前缀MY

$this->load->library('student_registration_form_validation');

我得到了一个不同的Unable to load the requested class: Student_registration_form_validation

【问题讨论】:

    标签: php forms codeigniter


    【解决方案1】:

    好的孩子们,我现在明白了。

    1. 使用前缀MY_ 或您在application/config/config.php 文件中声明的任何内容创建自定义表单验证类。例如,MY_Student_registration_form_validation 在 CI 3.x 的 application/libraries 内。

      $config['subclass_prefix'] = 'MY_';
      
    2. 在你的控制器中(我将在这里使用控制器,因为我没有在模型上测试过它),你可以通过以下方式加载这个自定义类:

      $this->load->library('my_student_registration_form_validation');
      
    3. 您现在可以通过以下方式使用它的方法:

      $this->my_student_registration_form_validation->validate_student();
      

    【讨论】:

      【解决方案2】:

      把这块放在APPPATH.'config/config.php'文件的末尾

      spl_autoload_register(function ($class) {
          //this block you can skip
          if (substr($class,0,3) !== 'CI_') {
              if (file_exists($file = APPPATH.'core/'.$class.'.php')) {
                  include $file;
              }
          }
      
          //this block is what you need
          if (substr($class,0,3) !== 'CI_') {
              if (file_exists($file = APPPATH.'libraries/'.$class.'.php')) {
                  include $file;
              }
          }
      });
      

      这样,您甚至不需要设置配置文件的预定义 MY_ 前缀。 将您的文件和类命名为:

      Student_registration_form_validation.php | Student_registration_form_validation
      Student_cancellation_form_validation.php | Student_cancellation_form_validation
      

      Some_controller_code.php

      <?php
      defined('BASEPATH') OR exit('No direct script access allowed');
      
      class Some_controller_code extends CI_Controller
      {
          public function __construct()
          {
              parent::__construct();
          }
      
          public function index()
          {
              //you have to load form_validation library first
              //if custom libraries are extending it
              $this->load->library('form_validation');
              $this->load->library('student_registration_form_validation');
      
              //example of using method from native form_validation library
              if ( $this->student_registration_form_validation->set_message('rule1', "Some message here") )
                  echo 'It\'s working';
          }
      }
      

      【讨论】:

      • 是否推荐用于生产?
      • 对不起,我试过了,但我的 problem of calling a callback inside a custom form validation class 仍然坚持你的解决方案。我现在只使用MY_Form_validation :-(
      • 回调是在控制器中创建的。你能用你如何使用它的代码来更新你的问题吗?
      • 它就在那里。 tl;博士我实际上在自定义表单验证类中制作并使用了回调。当类的名称不完全是 MY_Form_validation 时,它不起作用。
      • 您能发布在您的自定义MY_Form_validation 类中使用的workig 方法示例吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多