CI_Form_validation::set_rules PHP Method

set_rules() public method

This function takes an array of field names and validation rules as input, any custom error messages, validates the info, and stores it
public set_rules ( mixed $field, string $label = '', mixed $rules = [], array $errors = [] ) : CI_Form_validation
$field mixed
$label string
$rules mixed
$errors array
return CI_Form_validation
    public function set_rules($field, $label = '', $rules = array(), $errors = array())
    {
        // No reason to set rules if we have no POST data
        // or a validation array has not been specified
        if ($this->CI->input->method() !== 'post' && empty($this->validation_data)) {
            return $this;
        }
        // If an array was passed via the first parameter instead of individual 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'], $row['rules'])) {
                    continue;
                }
                // If the field label wasn't passed we use the field name
                $label = isset($row['label']) ? $row['label'] : $row['field'];
                // Add the custom error message array
                $errors = isset($row['errors']) && is_array($row['errors']) ? $row['errors'] : array();
                // Here we go!
                $this->set_rules($row['field'], $label, $row['rules'], $errors);
            }
            return $this;
        }
        // No fields or no rules? Nothing to do...
        if (!is_string($field) or $field === '' or empty($rules)) {
            return $this;
        } elseif (!is_array($rules)) {
            // BC: Convert pipe-separated rules string to an array
            if (!is_string($rules)) {
                return $this;
            }
            $rules = preg_split('/\\|(?![^\\[]*\\])/', $rules);
        }
        // If the field label wasn't passed we use the field name
        $label = $label === '' ? $field : $label;
        $indexes = array();
        // Is the field name an array? If it is an array, we break it apart
        // into its components so that we can fetch the corresponding POST data later
        if (($is_array = (bool) preg_match_all('/\\[(.*?)\\]/', $field, $matches)) === TRUE) {
            sscanf($field, '%[^[][', $indexes[0]);
            for ($i = 0, $c = count($matches[0]); $i < $c; $i++) {
                if ($matches[1][$i] !== '') {
                    $indexes[] = $matches[1][$i];
                }
            }
        }
        // Build our master array
        $this->_field_data[$field] = array('field' => $field, 'label' => $label, 'rules' => $rules, 'errors' => $errors, 'is_array' => $is_array, 'keys' => $indexes, 'postdata' => NULL, 'error' => '');
        return $this;
    }

Usage Example

 public function set_rules($field, $label = '', $rules = '')
 {
     if (is_array($field)) {
         foreach ($field as $row) {
             if (!isset($row['field']) or !isset($row['rules'])) {
                 continue;
             }
             $label = !isset($row['label']) ? $row['field'] : $row['label'];
             return $this->set_rules($row['field'], $label, $row['rules']);
         }
     }
     if (!is_string($field) or !is_string($rules) or $field == '') {
         return $this;
     }
     foreach (explode('|', $rules) as $rule) {
         if (strpos($rule, '[') !== FALSE) {
             $rule = current(explode('[', $field));
         }
         if (in_array($rule, $this->_rules)) {
             if (FALSE === ($line = $this->CI->lang->line($rule))) {
                 $line = 'Unable to access an error message corresponding to your field name.';
             }
             $this->rule_messages[$rule] = $line;
         }
     }
     $label = $label == '' ? $field : $label;
     $this->js_rules[] = array('name' => $field, 'display' => $label, 'rules' => $rules);
     return parent::set_rules($field, $label, $rules);
 }
All Usage Examples Of CI_Form_validation::set_rules