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.

[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 would be into the requested [b]Controller[/b] (the default CI's Form Validation behavior) [code] $this->form_validation->set_rules('authcode', '', 'callback__validate_authcode'); [/code]

[b]callback__validate_authcode_model[admin][/b] '_validate_authcode' callback would be into the [b]'admin' Model[/b] [code] $this->form_validation->set_rules('authcode', '', 'callback__validate_authcode_model[admin]'); [/code]

[b]callback__validate_authcode_model[public][/b] '_validate_authcode' callback would be into the [b]'public' Model[/b] [code] $this->form_validation->set_rules('authcode', '', 'callback__validate_authcode_model[public]'); [/code]

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

  1. Download the latest version.
  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 of 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]Original snippet of 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]

Clone this wiki locally