Welcome Guest, Not a member yet? Register   Sign In
Default rules for Form validation?
#1

[eluser]lifo101[/eluser]
I looked around a bit to see if this had been discussed in the past already but my limited searching revealed nothing related. I just started playing around with CI for one of my projects and my question is in regard to the Form Validation object.

I don't see a way to set default rules for form fields and I personally think this would be a nice addition. If it hasn't been done yet then I'll just extend the object and add my own set_default_rules() method to the class.

Think about it, almost all forms tend to at least 'trim' all input before processing, this is one of the primary cases where having default rules is useful. I just don't want to have to always add 'trim' to my rules (call me lazy).

- Jason
#2

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums.

I see where you're coming from, but I really think it's overkill. At the present time, it's obvious where to look if you want to see what rules are set for any given form input. Adding defaults gives you one extra place to look, and also adds complications in cases where default has been added that needs to be suppressed for whatever reason.

However, you're free to use use this script if you want. You set the default validation rules in your config.php file like this (and you have the option to override them if you wish using $this->config->set_item('default_validation_rules', '')):

Code:
$config['default_validation_rules'] = 'trim';

./system/application/libraries/MY_Form_validation.php
Code:
class MY_Form_validation extends CI_Form_validation {

    function MY_Form_validation($rules=array())
    {
        parent::CI_Form_validation($rules);
    }

    function set_rules($field, $label = '', $rules = '')
    {
        // No reason to set rules if we have no POST data
        if (count($_POST) == 0)
        {
            return;
        }
# START EDIT
        $default_rules = ($this->config->item('default_validation_rules')) ? '|' . $this->config->item('default_validation_rules') : '';
# END EDIT
        // If an array was passed via the first parameter instead of indidual string
        // values we cycle through it and recursively call this function.
        if (is_array($field))
        {
            foreach ($field as $row)
            {
                // Houston, we have a problem...
                if ( ! isset($row['field']) OR ! isset($row['rules']))
                {
                    continue;
                }

                // If the field label wasn't passed we use the field name
                $label = ( ! isset($row['label'])) ? $row['field'] : $row['label'];

                // Here we go!
# START EDIT
                $this->set_rules($row['field'], $label, $row['rules'] . $default_rules);
# END EDIT
            }
            return;
        }
        
        // No fields? Nothing to do...
        if ( ! is_string($field) OR  ! is_string($rules) OR $field == '')
        {
            return;
        }

        // If the field label wasn't passed we use the field name
        $label = ($label == '') ? $field : $label;

        // Is the field name an array?  We test for the existence of a bracket "[" in
        // the field name to determine this.  If it is an array, we break it apart
        // into its components so that we can fetch the corresponding POST data later        
        if (strpos($field, '[') !== FALSE AND preg_match_all('/\[(.*?)\]/', $field, $matches))
        {    
            // Note: Due to a bug in current() that affects some versions
            // of PHP we can not pass function call directly into it
            $x = explode('[', $field);
            $indexes[] = current($x);

            for ($i = 0; $i < count($matches['0']); $i++)
            {
                if ($matches['1'][$i] != '')
                {
                    $indexes[] = $matches['1'][$i];
                }
            }
            
            $is_array = TRUE;
        }
        else
        {
            $indexes     = array();
            $is_array    = FALSE;        
        }
        
        // Build our master array        
        $this->_field_data[$field] = array(
                                            'field'                => $field,
                                            'label'                => $label,
# START EDIT
                                            'rules'                => $rules . $default_rules,
# END EDIT
                                            'is_array'            => $is_array,
                                            'keys'                => $indexes,
                                            'postdata'            => NULL,
                                            'error'                => ''
                                            );
    }

The above code is untested.
#3

[eluser]lifo101[/eluser]
Thanks for the reply. I'll continue to contemplate this before doing anything. For now I'll force myself to explicitly set the rules as the current convention requires and maybe I'll lose the laziness Smile.




Theme © iAndrew 2016 - Forum software by © MyBB