This function takes an array of field names and validation
rules as input, any custom error messages, validates the info,
and stores it
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); }
/** *@desc Allow set a custom validation error message * */ public function set_rules($field, $label = '', $rules = '', $message = '') { $rules = parent::set_rules($field, $label, $rules); if (!empty($message)) { $this->_custom_field_errors[$field] = $message; } return $rules; }
public function set_rules($input_name, $input_name_display, $rule = NULL) { if ($rule == NULL) { $rule = $input_name_display; parent::set_rules($input_name, $input_name, $rule); } else { parent::set_rules($input_name, $input_name_display, $rule); } }
function set_rules($field, $label = '', $rules = '') { if (count($_POST) === 0 and count($_FILES) > 0) { //add a dummy $_POST $_POST['DUMMY_ITEM'] = ''; parent::set_rules($field, $label, $rules); unset($_POST['DUMMY_ITEM']); } else { //we are safe just run as is parent::set_rules($field, $label, $rules); } }
function set_rules($field, $label = '', $rules = array(), $errors = array()) { if (count($_POST) === 0 and count($_FILES) > 0) { //it will prevent the form_validation from working //add a dummy $_POST $_POST['DUMMY_ITEM'] = ''; parent::set_rules($field, $label, $rules); unset($_POST['DUMMY_ITEM']); } else { //we are safe just run as is parent::set_rules($field, $label, $rules); } }
public function set_rules($field, $label = '', $rules = '') { // No reason to set rules if we have no POST data if (count($_POST) == 0) { return $this; } if (is_array($field) && $field !== array_values($field)) { foreach ($field as $key => $value) { $label = !empty($value[1]) ? $value[1] : humanize($key); parent::set_rules($key, $label, $value[0]); } return $this; } parent::set_rules($field, $label, $rules); }
/** * Set Rules * * This function takes an array of field names and validation * rules as input, any custom error messages, validates the info, * and stores it * * @param mixed $field * @param string $label * @param mixed $rules * @param array $errors * @return CI_Form_validation */ public function set_rules($field, $label = '', $rules = array(), $errors = array()) { $this->request_method = $this->CI->input->method(); return parent::set_rules($field, $label, $rules, $errors); }
public function set_rules($field, $label = '', $rules = '') { $this->_add_field_name($field); parent::set_rules($field, $label, $rules); }
public function post() { // Validation result $result = array('validation' => FALSE); // Form name $form_name = $this->input->post('form_name'); // Form settings $form = $this->_get_form_settings($form_name); // Do not validate the form if the form is not defined if (is_null($form)) { $this->xhr_output(array()); } // If rules are defined in the config file... if (isset($form['fields'])) { $fields = $form['fields']; // Get each field settings foreach ($fields as $field => $settings) { if (isset($settings['rules'])) { $rules = $settings['rules']; $label = !empty($settings['label']) ? 'lang:' . $settings['label'] : $field; // See : http://codeigniter.com/user_guide/libraries/form_validation.html#translatingfn $this->form_validation->set_rules($field, $label, $rules); // User's callback rules // Callbacks rules cannot be executed by CI_Form_validation() // They are supposed to be $CI methods and we are here out of the scope of $CI /* * Not implemented for the moment * @todo: execute_validation_callback() needs to be rewritten $rules_array = explode('|', $rules); foreach($rules_array as $rule) { if (substr($rule, 0, 9) == 'callback_') { $row = array( 'field' => $field, 'label' => $label, 'rule' => $rule, 'post' => $this->input->post($field), ); $this->execute_validation_callback($row); } } */ } } // Check the rules $validation_passed = $this->form_validation->run(); // Error if (!$validation_passed) { $result['title'] = lang('form_alert_error_title'); $result['message'] = lang($form['messages']['error']); $result['errors'] = $this->form_validation->_error_array; } else { // Supposed to send back one array with 'title' and 'message' indexes $result = $this->_process_data($form); $result['validation'] = TRUE; if (!isset($result['title']) && !isset($result['message'])) { $result['title'] = lang('form_alert_success_title'); $result['message'] = lang($form['messages']['success']); } } } $this->xhr_output($result); }