Welcome Guest, Not a member yet? Register   Sign In
Variable Validation
#1

[eluser]bunal[/eluser]
Hi

As for as i know "Form Validation" and the oldie "Validation" is used by input fields.

What i want to do is to validate a variable.

Is there a way to use these classes to validate a variable instead of a field/input?
#2

[eluser]WanWizard[/eluser]
ExiteCMS uses this method.

To use it, extend the Form_validation library, and add it to the extension:
Code:
/**
     * Run validation rules on an array of values, or a single value
     *
     * @access    public
     * @param        mixed
     * @param        string
     * @param        string
     * @return        bool
     */
    function run_value($value = FALSE, $rules = '', $label = '')
    {
        // save the current error messages
        $errors = $this->_error_array;
        $this->_error_array = array();

        // save the current field data
        $fielddata = $this->_field_data;

        // create custom field data
        if ( is_array($value) )
        {
            $this->_field_data = array();
            foreach( $value as $key => $val )
            {
                if ( empty($val['label']) )
                {
                    $this->_field_data['_value'.$key.'_'] = array(
                            'field' => '_value'.$key.'_',
                            'label' => '',
                            'rules' => $val['rules'],
                            'keys' => is_array($val['value']) ? array_keys($val['value']) : array(),
                            'postdata' => $val['value'],
                            'is_array' => is_array($val['value']),
                            'error' => ''
                    );
                }
                else
                {
                    $this->_field_data[$val['label']] = array(
                            'field' => $val['label'],
                            'label' => $val['label'],
                            'rules' => $val['rules'],
                            'keys' => is_array($val['value']) ? array_keys($val['value']) : array(),
                            'postdata' => $val['value'],
                            'is_array' => is_array($val['value']),
                            'error' => ''
                    );
                }
            }
        }
        else
        {
            $field = empty($label) ? '_value_' : $label;
            $this->_field_data = array($field => array(
                    'field' => $field,
                    'label' => $label,
                    'rules' => $rules,
                    'keys' => is_array($value) ? array_keys($value) : array(),
                    'postdata' => $value,
                    'is_array' => is_array($value),
                    'error' => ''
                )
            );
        }

        // Load the language file containing error messages
        $this->CI->lang->load('form_validation');

        // Cycle through the rules for each field and test for errors
        foreach ($this->_field_data as $field => $row)
        {
            $this->_execute($row, explode('|', $row['rules']), $this->_field_data[$field]['postdata']);
        }

        // store the validation errors (if any)
        $this->run_value_errors = $this->_error_array;

        // restore the validation values
        $this->_error_array = $errors;
        $this->_field_data = $fielddata;

        // and return the validation result
        return count($this->run_value_errors) ? FALSE : TRUE;
    }
#3

[eluser]bunal[/eluser]
Thanks for the answer.

What do you think is the best practice?

To have all custom validation functions in a helper file like validation_helper.php and load the helper into MY_Form_Validation and use the functions in the MY_Form_Validation methods?

Or directly code the functions into MY_Form_Validation as methods and ignore validation_helper.php?
#4

[eluser]WanWizard[/eluser]
I have all 'generic' validation methods in my MY_Form_validation library.

I use Modular CI to split my applications functionality in modules. Module specific rules are in a separate module library. One of Modular CI's features allows you to tell the Form validation library which object contains the callback rules, I use that to point to the rules library.




Theme © iAndrew 2016 - Forum software by © MyBB