/** *Custom Form Validation Error Messages *http://ajmm.org/2011/07/custom-form-validation-error-messages-in-codeigniter-2/ */ public function _execute($row, $rules, $postdata = NULL, $cycles = 0) { // Execute the parent method from CI_Form_validation. parent::_execute($row, $rules, $postdata, $cycles); // Override any error messages for the current field. if (isset($this->_error_array[$row['field']]) && isset($this->_custom_field_errors[$row['field']])) { $message = str_replace('%s', !empty($row['label']) ? $row['label'] : $row['field'], $this->_custom_field_errors[$row['field']]); $this->_error_array[$row['field']] = $message; $this->_field_data[$row['field']]['error'] = $message; } }
public function _execute($row, $rules, $postdata = NULL, $cycles = 0) { if (!is_array($postdata) && strlen($postdata) == 0 || is_array($postdata) && empty($postdata)) { $postdata = NULL; } return parent::_execute($row, $rules, $postdata, $cycles); }
function _execute($row, $rules, $postdata = NULL, $cycles = 0) { log_message('DEBUG', 'called MY_form_validation::_execute ' . $row['field']); //changed based on //http://codeigniter.com/forums/viewthread/123816/P10/#619868 if (isset($_FILES[$row['field']])) { // it is a file so process as a file log_message('DEBUG', 'processing as a file'); $postdata = $_FILES[$row['field']]; //required bug //if some stupid like me never remember that it's file_required and not required //this will save a lot of var_dumping time. if (in_array('required', $rules)) { $rules[array_search('required', $rules)] = 'file_required'; } //before doing anything check for errors if ($postdata['error'] !== UPLOAD_ERR_OK) { //If the error it's 4 (ERR_NO_FILE) and the file required it's deactivated don't call an error if ($postdata['error'] != UPLOAD_ERR_NO_FILE) { $this->_error_array[$row['field']] = $this->file_upload_error_message($row['label'], $postdata['error']); $this->_field_data[$row['field']]['error'] = $this->file_upload_error_message($row['label'], $postdata['error']); return FALSE; } elseif ($postdata['error'] == UPLOAD_ERR_NO_FILE and in_array('file_required', $rules)) { $this->_error_array[$row['field']] = $this->file_upload_error_message($row['label'], $postdata['error']); $this->_field_data[$row['field']]['error'] = $this->file_upload_error_message($row['label'], $postdata['error']); return FALSE; } } $_in_array = FALSE; // If the field is blank, but NOT required, no further tests are necessary $callback = FALSE; if (!in_array('file_required', $rules) and $postdata['size'] == 0) { // Before we bail out, does the rule contain a callback? if (preg_match("/(callback_\\w+)/", implode(' ', $rules), $match)) { $callback = TRUE; $rules = array('1' => $match[1]); } else { return; } } foreach ($rules as $rule) { /// COPIED FROM the original class // Is the rule a callback? $callback = $callable = FALSE; if (is_string($rule)) { if (strpos($rule, 'callback_') === 0) { $rule = substr($rule, 9); $callback = TRUE; } } elseif (is_callable($rule)) { $callable = TRUE; } // Strip the parameter (if exists) from the rule // Rules can contain a parameter: max_length[5] $param = FALSE; if (!$callable && preg_match('/(.*?)\\[(.*)\\]/', $rule, $match)) { $rule = $match[1]; $param = $match[2]; } // Call the function that corresponds to the rule if ($callback or $callable) { if ($callback) { if (!method_exists($this->CI, $rule)) { log_message('debug', 'Unable to find callback validation rule: ' . $rule); $result = FALSE; } else { // Run the function and grab the result $result = $this->CI->{$rule}($postdata, $param); } } else { $result = is_array($rule) ? $rule[0]->{$rule[1]}($postdata, $param) : $rule($postdata, $param); } // Re-assign the result to the master data array if ($_in_array == TRUE) { $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result; } else { $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result; } // If the field isn't required and we just processed a callback we'll move on... if (!in_array('file_required', $rules, TRUE) and $result !== FALSE) { return; } } elseif (!method_exists($this, $rule)) { // If our own wrapper function doesn't exist we see if a native PHP function does. // Users can use any native PHP function call that has one param. if (function_exists($rule)) { // Native PHP functions issue warnings if you pass them more parameters than they use $result = $param !== FALSE ? $rule($postdata, $param) : $rule($postdata); if ($_in_array === TRUE) { $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result; } else { $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result; } } else { log_message('debug', 'Unable to find validation rule: ' . $rule); $result = FALSE; } } else { $result = $this->{$rule}($postdata, $param); if ($_in_array === TRUE) { $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result; } else { $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result; } } // Did the rule test negatively? If so, grab the error. if ($result === FALSE) { // Check if a custom message is defined if (isset($this->_field_data[$row['field']]['errors'][$rule])) { $line = $this->_field_data[$row['field']]['errors'][$rule]; } elseif (!isset($this->_error_messages[$rule])) { if (FALSE === ($line = $this->CI->lang->line('form_validation_' . $rule)) && FALSE === ($line = $this->CI->lang->line($rule, FALSE))) { $line = 'Unable to access an error message corresponding to your field name.'; } } else { $line = $this->_error_messages[$rule]; } // Is the parameter we are inserting into the error message the name // of another field? If so we need to grab its "field label" if (isset($this->_field_data[$param], $this->_field_data[$param]['label'])) { $param = $this->_translate_fieldname($this->_field_data[$param]['label']); } // Build the error message $message = $this->_build_error_msg($line, $this->_translate_fieldname($row['label']), $param); // Save the error message $this->_field_data[$row['field']]['error'][] = $message; $this->_error_array[$row['field']][] = $message; } } } else { log_message('DEBUG', 'Called parent _execute'); parent::_execute($row, $rules, $postdata, $cycles); } }
function _execute($row, $rules, $postdata = NULL, $cycles = 0) { log_message('DEBUG', 'called Brightery_form_validation::_execute ' . $row['field']); if (isset($_FILES[$row['field']])) { // it is a file so process as a file log_message('DEBUG', 'processing as a file'); $postdata = $_FILES[$row['field']]; //required bug //if some stupid like me never remember that it's file_required and not required //this will save a lot of var_dumping time. if (in_array('required', $rules)) { $rules[array_search('required', $rules)] = 'file_required'; } //before doing anything check for errors if ($postdata['error'] !== UPLOAD_ERR_OK) { //If the error it's 4 (ERR_NO_FILE) and the file required it's deactivated don't call an error if ($postdata['error'] != UPLOAD_ERR_NO_FILE) { $this->_error_array[$row['field']] = $this->file_upload_error_message($row['label'], $postdata['error']); return FALSE; } elseif ($postdata['error'] == UPLOAD_ERR_NO_FILE and in_array('file_required', $rules)) { $this->_error_array[$row['field']] = $this->file_upload_error_message($row['label'], $postdata['error']); return FALSE; } } $_in_array = FALSE; // If the field is blank, but NOT required, no further tests are necessary $callback = FALSE; if (!in_array('file_required', $rules) and $postdata['size'] == 0) { // Before we bail out, does the rule contain a callback? if (preg_match("/(callback_\\w+)/", implode(' ', $rules), $match)) { $callback = TRUE; $rules = array('1' => $match[1]); } else { return; } } foreach ($rules as $rule) { /// COPIED FROM the original class // Is the rule a callback? $callback = FALSE; if (substr($rule, 0, 9) == 'callback_') { $rule = substr($rule, 9); $callback = TRUE; } // Strip the parameter (if exists) from the rule // Rules can contain a parameter: max_length[5] $param = FALSE; if (preg_match("/(.*?)\\[(.*?)\\]/", $rule, $match)) { $rule = $match[1]; $param = $match[2]; } // Call the function that corresponds to the rule if ($callback === TRUE) { if (!method_exists($this->CI, $rule)) { continue; } // Run the function and grab the result $result = $this->CI->{$rule}($postdata, $param); // Re-assign the result to the master data array if ($_in_array == TRUE) { $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result; } else { $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result; } // If the field isn't required and we just processed a callback we'll move on... if (!in_array('file_required', $rules, TRUE) and $result !== FALSE) { return; } } else { if (!method_exists($this, $rule)) { // If our own wrapper function doesn't exist we see if a native PHP function does. // Users can use any native PHP function call that has one param. if (function_exists($rule)) { $result = $rule($postdata); if ($_in_array == TRUE) { $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result; } else { $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result; } } continue; } $result = $this->{$rule}($postdata, $param); if ($_in_array == TRUE) { $this->_field_data[$row['field']]['postdata'][$cycles] = is_bool($result) ? $postdata : $result; } else { $this->_field_data[$row['field']]['postdata'] = is_bool($result) ? $postdata : $result; } } //this line needs testing !!!!!!!!!!!!! not sure if it will work //it basically puts back the tested values back into $_FILES //$_FILES[$row['field']] = $this->_field_data[$row['field']]['postdata']; // Did the rule test negatively? If so, grab the error. if ($result === FALSE) { if (!isset($this->_error_messages[$rule])) { if (FALSE === ($line = $this->CI->lang->line($rule))) { $line = 'Unable to access an error message corresponding to your field name.'; } } else { $line = $this->_error_messages[$rule]; } // Is the parameter we are inserting into the error message the name // of another field? If so we need to grab its "field label" if (isset($this->_field_data[$param]) && isset($this->_field_data[$param]['label'])) { $param = $this->_field_data[$param]['label']; } // Build the error message $message = sprintf($line, $this->_translate_fieldname($row['label']), $param); // Save the error message $this->_field_data[$row['field']]['error'] = $message; $this->_error_array[$row['field']][] = $message; return; } } } else { log_message('DEBUG', 'Called parent _execute'); parent::_execute($row, $rules, $postdata, $cycles); } }