MY Form validation Simple Callbacks into Models

Derek Jones edited this page Jul 5, 2012 · 18 revisions
Clone this wiki locally


[size=6]→ IMPORTANT! DON'T USE THIS[/size]

This code is from June 2009. It is as of now (December 2011) untested and unmantained.

I recommend using a simpler solution that doesn't imply hacking any core file:

CI 2.1.0 form validation external callbacks via the newly available param (by skunkbad) http://codeigniter.com/forums/viewthread/205469/


[size=6]Description[/size]

The aim of this library is to allow a simple (simplistic!) way of storing Validation Callbacks into Models. With minimal modifications to the Form validation library and resembling as much as possible the CI conventions for validation rules.

Once installed the library you will only need to append _model[your_model_name] to your callback name, where your_model_name indicates the model which stores the callback function.

If no model is specified, it is assumed that the callback resides in the requested Controller, as CodeIgniter Form validation library behaves by default.

[size=6]Example[/size]

Let's say we want to assign the _validate_authcode callback to the authcode form field.

callback__validate_authcode _validate_authcode callback is located in the requested Controller (the default CI's callback behavior)


$this->form_validation->set_rules('authcode', 'Authorization Code', 'callback__validate_authcode');

callback__validate_authcode_model[admin] _validate_authcode callback is located in the 'admin' Model


$this->form_validation->set_rules('authcode', 'Authorization Code', 'callback__validate_authcode_model[admin]'); 

callback__validate_authcode_model[public] _validate_authcode callback is located in the 'public' Model


$this->form_validation->set_rules('authcode', 'Authorization Code', 'callback__validate_authcode_model[public]'); 

[size=6]Installation[/size]

  1. Download the latest version
  2. Copy it to your application libraries folder (example: application/libraries/MY_Form_validation.php)

[size=6]Source[/size]

NOTE: This is not the whole library source code. Use the downloadable version instead!

Snippet from MY_Form_validation.php


// Call the function that corresponds to the rule
if ($callback === TRUE)
{
        // >>> START of modificaction

        $model = FALSE;

        if (strpos($rule, '_model') AND $param)
        {
           $model = $param;
           $rule = substr($rule, 0, -6);
        }

        // Is the callback into a model?
        if ($model)
        {
            if ( ! method_exists($this->CI->$model, $rule))
            {
                continue;
            }

            // Run the function and grab the result
            $result = $this->CI->$model->$rule($postdata);
        }
        else
        {
            if ( ! method_exists($this->CI, $rule))
            {
                continue;
            }

            // Run the function and grab the result
            $result = $this->CI->$rule($postdata, $param);
        }

        // <<< END of modification

        // Re-assign the result to the master data array
        if ($_in_array == TRUE)
        {
                $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
        }
        else
        {
                $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
        }

        // If the field isn't required and we just processed a callback we'll move on...
        if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
        {
                return;
        }
}

Snippet from CI's native Form_validation.php (Lines 580 - 606)


// Call the function that corresponds to the rule
if ($callback === TRUE)
{
        if ( ! method_exists($this->CI, $rule))
        {         
                continue;
        }

        // Run the function and grab the result
        $result = $this->CI->$rule($postdata, $param);

        // Re-assign the result to the master data array
        if ($_in_array == TRUE)
        {
                $this->_field_data[$row['field']]['postdata'][$cycles] = (is_bool($result)) ? $postdata : $result;
        }
        else
        {
                $this->_field_data[$row['field']]['postdata'] = (is_bool($result)) ? $postdata : $result;
        }

        // If the field isn't required and we just processed a callback we'll move on...
        if ( ! in_array('required', $rules, TRUE) AND $result !== FALSE)
        {
                return;
        }
}

[size=6]Resources[/size]

Forum announcement Source browser

Category:Libraries::Validation | Category:Libraries::Model