Skip to content

MY Form validation Simple Callbacks into Models

World Wide Web Server edited this page Jul 4, 2012 · 18 revisions

[size=6][color=#F93D00]Description[/color][/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 [b]_model[your_model_name][/b] to your callback name, where [b]your_model_name[/b] 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][color=#F93D00]Example[/color][/size]

Let's say we want to assign the [b]_validate_authcode[/b] callback to the [b]authcode[/b] form field.

[b]callback__validate_authcode[/b] _validate_authcode callback is located in the requested [b]Controller[/b] (the default CI's callback behavior) [code] $this->form_validation->set_rules('authcode', 'Authorization Code', 'callback__validate_authcode'); [/code]

[b]callback__validate_authcode_model[admin][/b] _validate_authcode callback is located in the [b]'admin' Model[/b] [code] $this->form_validation->set_rules('authcode', 'Authorization Code', 'callback__validate_authcode_model[admin]'); [/code]

[b]callback__validate_authcode_model[public][/b] _validate_authcode callback is located in the [b]'public' Model[/b] [code] $this->form_validation->set_rules('authcode', 'Authorization Code', 'callback__validate_authcode_model[public]'); [/code]

[size=6][color=#F93D00]Installation[/color][/size]

  1. [url=http://bit.ly/codeigniter-simple-callbacks-into-models]Download the latest version[/url]
  2. Copy it to your application libraries folder (example: application/libraries/MY_Form_validation.php)

[size=6][color=#F93D00]Source[/color][/size]

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

[b]Snippet from MY_Form_validation.php[/b]

[code] // 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;
    }

} [/code]

[b]Snippet from CI's native Form_validation.php (Lines 580 - 606)[/b]

[code] // 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;
    }

} [/code]

[size=6][color=#F93D00]Resources[/color][/size]

[url=http://codeigniter.com/forums/viewthread/123780/]Forum announcement[/url] [url=http://code.google.com/p/margenn-ci-resources/source/browse/trunk/libraries/MY_Form_validation.php]Source browser[/url]

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

Clone this wiki locally