Skip to content

Commit

Permalink
🔨 improve form validation
Browse files Browse the repository at this point in the history
Signed-off-by: otengkwame <developerkwame@gmail.com>
  • Loading branch information
otengkwame committed Jun 4, 2022
1 parent 6d48e98 commit 21c714f
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 52 deletions.
107 changes: 55 additions & 52 deletions framework/language/english/form_validation_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,64 +35,67 @@
* @since Version 1.0.0
* @filesource
*/
defined('BASEPATH') OR exit('No direct script access allowed');
defined('BASEPATH') or exit('No direct script access allowed');

$lang['form_validation_required'] = 'The {field} field is required.';
$lang['form_validation_isset'] = 'The {field} field must have a value.';
$lang['form_validation_valid_email'] = 'The {field} field must contain a valid email address.';
$lang['form_validation_valid_emails'] = 'The {field} field must contain all valid email addresses.';
$lang['form_validation_valid_url'] = 'The {field} field must contain a valid URL.';
$lang['form_validation_valid_ip'] = 'The {field} field must contain a valid IP.';
$lang['form_validation_valid_base64'] = 'The {field} field must contain a valid Base64 string.';
$lang['form_validation_min_length'] = 'The {field} field must be at least {param} characters in length.';
$lang['form_validation_max_length'] = 'The {field} field cannot exceed {param} characters in length.';
$lang['form_validation_exact_length'] = 'The {field} field must be exactly {param} characters in length.';
$lang['form_validation_alpha'] = 'The {field} field may only contain alphabetical characters.';
$lang['form_validation_alpha_numeric'] = 'The {field} field may only contain alpha-numeric characters.';
$lang['form_validation_alpha_numeric_spaces'] = 'The {field} field may only contain alpha-numeric characters and spaces.';
$lang['form_validation_alpha_dash'] = 'The {field} field may only contain alpha-numeric characters, underscores, and dashes.';
$lang['form_validation_numeric'] = 'The {field} field must contain only numbers.';
$lang['form_validation_is_numeric'] = 'The {field} field must contain only numeric characters.';
$lang['form_validation_integer'] = 'The {field} field must contain an integer.';
$lang['form_validation_regex_match'] = 'The {field} field is not in the correct format.';
$lang['form_validation_matches'] = 'The {field} field does not match the {param} field.';
$lang['form_validation_differs'] = 'The {field} field must differ from the {param} field.';
$lang['form_validation_is_unique'] = 'The {field} field must contain a unique value.';
$lang['form_validation_is_natural'] = 'The {field} field must only contain digits.';
$lang['form_validation_is_natural_no_zero'] = 'The {field} field must only contain digits and must be greater than zero.';
$lang['form_validation_decimal'] = 'The {field} field must contain a decimal number.';
$lang['form_validation_less_than'] = 'The {field} field must contain a number less than {param}.';
$lang['form_validation_less_than_equal_to'] = 'The {field} field must contain a number less than or equal to {param}.';
$lang['form_validation_greater_than'] = 'The {field} field must contain a number greater than {param}.';
$lang['form_validation_greater_than_equal_to'] = 'The {field} field must contain a number greater than or equal to {param}.';
$lang['form_validation_error_message_not_set'] = 'Unable to access an error message corresponding to your field name {field}.';
$lang['form_validation_in_list'] = 'The {field} field must be one of: {param}.';
$lang['form_validation_required'] = 'The {field} field is required.';
$lang['form_validation_isset'] = 'The {field} field must have a value.';
$lang['form_validation_valid_email'] = 'The {field} field must contain a valid email address.';
$lang['form_validation_valid_emails'] = 'The {field} field must contain all valid email addresses.';
$lang['form_validation_valid_url'] = 'The {field} field must contain a valid URL.';
$lang['form_validation_valid_ip'] = 'The {field} field must contain a valid IP.';
$lang['form_validation_valid_base64'] = 'The {field} field must contain a valid Base64 string.';
$lang['form_validation_min_length'] = 'The {field} field must be at least {param} characters in length.';
$lang['form_validation_max_length'] = 'The {field} field cannot exceed {param} characters in length.';
$lang['form_validation_exact_length'] = 'The {field} field must be exactly {param} characters in length.';
$lang['form_validation_alpha'] = 'The {field} field may only contain alphabetical characters.';
$lang['form_validation_alpha_numeric'] = 'The {field} field may only contain alpha-numeric characters.';
$lang['form_validation_alpha_numeric_spaces'] = 'The {field} field may only contain alpha-numeric characters and spaces.';
$lang['form_validation_alpha_dash'] = 'The {field} field may only contain alpha-numeric characters, underscores, and dashes.';
$lang['form_validation_numeric'] = 'The {field} field must contain only numbers.';
$lang['form_validation_is_numeric'] = 'The {field} field must contain only numeric characters.';
$lang['form_validation_integer'] = 'The {field} field must contain an integer.';
$lang['form_validation_regex_match'] = 'The {field} field is not in the correct format.';
$lang['form_validation_matches'] = 'The {field} field does not match the {param} field.';
$lang['form_validation_differs'] = 'The {field} field must differ from the {param} field.';
$lang['form_validation_is_unique'] = 'The {field} field must contain a unique value.';
$lang['form_validation_exists'] = 'The {field} field exists already.';
$lang['form_validation_is_natural'] = 'The {field} field must only contain digits.';
$lang['form_validation_is_natural_no_zero'] = 'The {field} field must only contain digits and must be greater than zero.';
$lang['form_validation_decimal'] = 'The {field} field must contain a decimal number.';
$lang['form_validation_less_than'] = 'The {field} field must contain a number less than {param}.';
$lang['form_validation_less_than_equal_to'] = 'The {field} field must contain a number less than or equal to {param}.';
$lang['form_validation_greater_than'] = 'The {field} field must contain a number greater than {param}.';
$lang['form_validation_greater_than_equal_to'] = 'The {field} field must contain a number greater than or equal to {param}.';
$lang['form_validation_error_message_not_set'] = 'Unable to access an error message corresponding to your field name {field}.';
$lang['form_validation_in_list'] = 'The {field} field must be one of: {param}.';
$lang['form_validation_honey_check'] = 'The {field} field must be filled with style.';
$lang['form_validation_honey_time'] = 'The {field} field may only be needed once.';
$lang['form_validation_is_exactly'] = "The {field} field does not contain expected values.";
$lang['form_validation_is_not'] = "The {field} field does not accept entered value.";
$lang['form_validation_valid_json'] = "The {field} field must be a valid json.";
$lang['form_validation_valid_hour'] = "The {field} field must be a valid hour.";
$lang['form_validation_valid_date'] = "The {field} field must be a valid date.";
$lang['form_validation_valid_range_date'] = "The {field} field must be a valid date range.";
$lang['form_validation_valid_latitude'] = "The %s latitude doesn't have a correct position.";
$lang['form_validation_valid_longitude'] = "The %s longitude doesn't have a correct position.";
$lang['form_validation_valid_latlong'] = "The %s map coodinates doesn't have a correct position.";

// --------------------------------------------------------------------

$lang['file_required'] = 'The field {field} is required.';
$lang['file_size_max'] = "The file uploaded in {field} is too big (max size is {param}).";
$lang['file_size_min'] = "The file uploaded in {field} is too small (min size is {param}).";
$lang['file_allowed_type'] = "The file uploaded in {field} should be {param}.";
$lang['file_disallowed_type'] = "The file uploaded in {field} cannot be {param}.";
$lang['file_image_maxdim'] = "The dimensions of the file uploaded in {field} are too big.";
$lang['file_image_mindim'] = "The dimensions of the file uploaded in {field} are too small.";
$lang['file_image_exactdim'] = "The file uploaded in {field} doesn't have the right dimensions.";
$lang['is_exactly'] = "The field {field} contains a non valid value.";
$lang['is_not'] = "The field {field} contains a non valid value.";
$lang['error_max_filesize_phpini'] = "The file exceeds the upload_max_filesize directive from php.ini.";
$lang['error_max_filesize_form'] = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.";
$lang['error_partial_upload'] = "The file it's only partially uploaded.";
$lang['error_temp_dir'] = "Temp dir error.";
$lang['file_required'] = 'The {field} field needs a file to upload.';
$lang['file_max_size'] = "The {field} file is too big (max size is {param}).";
$lang['file_min_size'] = "The {field} file is too small (min size is {param}).";
$lang['file_allowed_type'] = "The {field} file allowed should be a/an {param}.";
$lang['file_disallowed_type'] = "The {field} file type {param}, is not allowed.";
$lang['file_image_maxdim'] = "The dimensions of the {field} file are too big.";
$lang['file_image_mindim'] = "The dimensions of the {field} file are too small.";
$lang['file_image_exactdim'] = "The {field} file doesn't have the right dimensions.";
$lang['error_max_filesize_phpini'] = "The uploaded file exceeds the maximum upload size of PHP."; // from php.ini
$lang['error_max_filesize_form'] = "The uploaded file exceeds the MAX_FILE_SIZE allowed."; // from form validation
$lang['error_partial_upload'] = "The file is only partially uploaded.";
$lang['error_temp_dir'] = "Temp directory error.";
$lang['error_disk_write'] = "Disk write error.";
$lang['error_stopped'] = "File upload stopped by extension";
$lang['error_unexpected'] = "Unexpected file upload error. Error: {field}";
$lang['valid_json'] = "The field {field} must be a valid json.";
$lang['valid_hour'] = "The field {field} must be a valid hour.";
$lang['valid_date'] = "The field {field} must be a valid date.";
$lang['valid_range_date'] = "The field {field} must be a valid date range.";
$lang['valid_latitude'] = "The %s latitude doesn't have a correct position.";
$lang['valid_longitude'] = "The %s longitude doesn't have a correct position.";
$lang['valid_latlong'] = "The %s map coodinates doesn't have a correct position.";
$lang['honey_check'] = 'The {field} field must be filled with style.';
$lang['honey_time'] = 'The {field} field may only be needed once.';
60 changes: 60 additions & 0 deletions framework/libraries/Form_validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,38 @@ public function set_rules($field, $label = '', $rules = [], $errors = [])
return $this;
}

/**
* Alias to the above method
*
* 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 rules($field, $label = '', $rules = [], $errors = [])
{
return $this->set_data($field, $label, $rules, $errors);
}

/**
* Alias to the above method
*
* @param mixed $field
* @param string $label
* @param array $rules
* @param array $errors
* @return CI_Form_validation
*/
public function rule($field, $label = '', $rules = [], $errors = [])
{
return $this->rules($field, $label, $rules, $errors);
}

// --------------------------------------------------------------------

/**
Expand Down Expand Up @@ -303,6 +335,21 @@ public function set_message($lang, $val = '')
return $this;
}

/**
* Alias to the method above
*
* Lets users set their own error messages on the fly. Note:
* The key name has to match the function name that it corresponds to.
*
* @param array
* @param string
* @return CI_Form_validation
*/
public function setMessage($lang, $val = '')
{
return $this->set_message($lang, $val);
}

// --------------------------------------------------------------------

/**
Expand Down Expand Up @@ -492,6 +539,19 @@ public function run($group = '')
return ($total_errors === 0);
}

/**
* Alias to the method above
*
* This function does all the work.
*
* @param string $group
* @return bool
*/
public function check($group = '')
{
return $this->run($group);
}

// --------------------------------------------------------------------

/**
Expand Down

0 comments on commit 21c714f

Please sign in to comment.