Welcome Guest, Not a member yet? Register   Sign In
Create multiple subclasses of Form Validation + callback inside should work
#1

This is a two-part problem because I believe the issues are dependent on each other.

What I want to achieve:

I am creating a site with multiple specific forms and I want each of them to have their (1) own validation class instead of a bloated MY_Form_validation.php.

(2) Callbacks will also happen inside that validation class and not within the form's controller.

OK CASE:

I create MY_Form_validation.php with a callback method called "check_value":

PHP Code:
<?php
class MY_Form_validation extends CI_Form_validation
{
 
   protected $CI;

 
   public function __construct($rules = array())
 
   {
 
       parent::__construct($rules);
 
       $this->CI =& get_instance();
 
   }

 
   public function validate()
 
   {
 
       $this->CI->form_validation->set_rules('value''value',
 
           'trim|required|integer|check_value['.$this->CI->input->post('unit').']');
 
       $this->CI->form_validation->set_rules('unit''unit''trim|required');
 
   }

 
   public function check_value($value$unit)
 
   {
 
       if($value >= 100)
 
       {
 
           return TRUE;
 
       }
 
       else
        
{
 
           $this->CI->form_validation->set_message('check_value',
 
               'The %s must be greater than or equal 100.');
 
           return FALSE;
 
       }
 
   
 
check_value is called inside validate() and it is called without the "callback_" prefix. It works that way.

This validation class is used in the form controller as:

PHP Code:
public function __construct()
{
 
   parent::__construct();
 
   $this->load->helper(array('form''url'));
 
   $this->load->library('form_validation'); 


and the method called:

PHP Code:
$this->form_validation->validate(); 


NOK CASE:

Same settings with OK CASE, just with a classname like "Car_registration_form_validation" and the manner of usage:

PHP Code:
public function __construct()
{
 
   parent::__construct();
 
   $this->load->helper(array('form''url'));
 
   $this->load->library('form_validation');
 
   $this->load->library('car_registration_form_validation');


PHP Code:
$this->car_registration_form_validation->validate(); 

With the settings above, I get the following error:

Code:
Unable to access an error message corresponding to your field name my_field.(check_value)

My questions:

  1. Is it possible to create more than one (aside from MY_Form_validation) form validation subclasses?
  2. Is my way of calling the callback correct in the NOK CASE? Does it need a special way since its class name is "MY_Form_validation" (the only discrepancy I see)?
Reply


Messages In This Thread
Create multiple subclasses of Form Validation + callback inside should work - by 678238515702801 - 09-04-2016, 08:31 PM



Theme © iAndrew 2016 - Forum software by © MyBB