Welcome Guest, Not a member yet? Register   Sign In
Using Custom Validation with CI
#1

[eluser]Unknown[/eluser]
I know about the callback_ function in the validation, but my scenario is a little different. I want to run every incoming value through this function, and if the value turns out to be invalid, then I'd like to somehow mark it invalid and use CI to handle the errors.

Code:
// Checks to make sure any data input during the project building process is valid
    function _check_entry($key, $value, $method, $step) {
        $blueprint = array(
            'method'            => array('art','scan','project'),
            'barcode'            => array('yes','no'),
            'fabric_type'            => array('soft','hard'),
            'color'                    => array('blue','red'),
            'address_1'            => '',
            'address_2'            => ''
        );
        
        $required = array(
            'method',
            'barcode',
            'fabric_type',
            'color',
            'address_1'
        );
        
        $char_limit = array(
            'description'    => '128'
        );
        
        $errors = array();

        // First, we need to make sure the k/v coming in exists in the blueprint
        
        if (array_key_exists($key, $blueprint)) {
            echo "The 'key' $key is in the blueprint<br />";
            if (in_array($key, $required)) {
                //key is required
                echo "$key is identified as a required value<br />";
                if ($blueprint["$key"] !== '') {
                    echo "$key is identified as a preset value<br />";
                    // needs to be a preset value
                    if (!in_array($value, $blueprint[$key])) {
                        // User provided info other than preset value, invalid
                        $errors["$key"] = 'We think our form works fine the way it was built, but thanks.';
                    } elseif ($value == '') {
                        // Data is missing, invalid
                        $errors["$key"] = 'Psst! You need to fill in this section.';
                    } else {
                        // Data is valid
                        echo "$value is a valid value for $key<br />";
                    }
                                        
                } else {
                    // is a user defined valued
                    if ($value !== '') {
                        // Data is provided, see if we need to impose a character limit
                        if (array_key_exists($key, $char_limit)) {
                            if (strlen($value) > $char_limit[$key]) {
                                // Data exceeds character limit, invalid
                                $errors["$key"] = "You exceeded the length limit for $key";
                            }
                        } else {
                            // Data is valid
                        }
                        
                    } else {
                        // User data is empty, invalid
                        $errors["$key"] = "Woops! Looks like you forgot $key";
                    }
                }    
            } else {
                // Key is not required, so assume valid since it is in the blueprint
            }
        } else {
        // Incoming key doesn't exist in the blueprint, return error.
            $errors["$key"] = "That key doesn't exist in the bluprint.";
            $errors["$key"] = 'We think our form works fine the way it was built, but thanks.';
        }
        
        //var_dump($errors);die;                
        
        if (empty($errors)) {
            return TRUE;
        } else {
            return $errors;
        }
    }

I'm really not sure what is the best approach on this, so I'd be interested in hearing everyone's thoughts. I am building a multistep form, so having different validation rules for each form can get out of control really fast. I wrote the above function as a general handler along with some custom code for any scenario in which I need to check special things, like character limits.

Any ideas?




Theme © iAndrew 2016 - Forum software by © MyBB