Skip to content
This repository was archived by the owner on Nov 18, 2025. It is now read-only.

Commit 21c714f

Browse files
committed
🔨 improve form validation
Signed-off-by: otengkwame <[email protected]>
1 parent 6d48e98 commit 21c714f

File tree

2 files changed

+115
-52
lines changed

2 files changed

+115
-52
lines changed

framework/language/english/form_validation_lang.php

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -35,64 +35,67 @@
3535
* @since Version 1.0.0
3636
* @filesource
3737
*/
38-
defined('BASEPATH') OR exit('No direct script access allowed');
38+
defined('BASEPATH') or exit('No direct script access allowed');
3939

40-
$lang['form_validation_required'] = 'The {field} field is required.';
41-
$lang['form_validation_isset'] = 'The {field} field must have a value.';
42-
$lang['form_validation_valid_email'] = 'The {field} field must contain a valid email address.';
43-
$lang['form_validation_valid_emails'] = 'The {field} field must contain all valid email addresses.';
44-
$lang['form_validation_valid_url'] = 'The {field} field must contain a valid URL.';
45-
$lang['form_validation_valid_ip'] = 'The {field} field must contain a valid IP.';
46-
$lang['form_validation_valid_base64'] = 'The {field} field must contain a valid Base64 string.';
47-
$lang['form_validation_min_length'] = 'The {field} field must be at least {param} characters in length.';
48-
$lang['form_validation_max_length'] = 'The {field} field cannot exceed {param} characters in length.';
49-
$lang['form_validation_exact_length'] = 'The {field} field must be exactly {param} characters in length.';
50-
$lang['form_validation_alpha'] = 'The {field} field may only contain alphabetical characters.';
51-
$lang['form_validation_alpha_numeric'] = 'The {field} field may only contain alpha-numeric characters.';
52-
$lang['form_validation_alpha_numeric_spaces'] = 'The {field} field may only contain alpha-numeric characters and spaces.';
53-
$lang['form_validation_alpha_dash'] = 'The {field} field may only contain alpha-numeric characters, underscores, and dashes.';
54-
$lang['form_validation_numeric'] = 'The {field} field must contain only numbers.';
55-
$lang['form_validation_is_numeric'] = 'The {field} field must contain only numeric characters.';
56-
$lang['form_validation_integer'] = 'The {field} field must contain an integer.';
57-
$lang['form_validation_regex_match'] = 'The {field} field is not in the correct format.';
58-
$lang['form_validation_matches'] = 'The {field} field does not match the {param} field.';
59-
$lang['form_validation_differs'] = 'The {field} field must differ from the {param} field.';
60-
$lang['form_validation_is_unique'] = 'The {field} field must contain a unique value.';
61-
$lang['form_validation_is_natural'] = 'The {field} field must only contain digits.';
62-
$lang['form_validation_is_natural_no_zero'] = 'The {field} field must only contain digits and must be greater than zero.';
63-
$lang['form_validation_decimal'] = 'The {field} field must contain a decimal number.';
64-
$lang['form_validation_less_than'] = 'The {field} field must contain a number less than {param}.';
65-
$lang['form_validation_less_than_equal_to'] = 'The {field} field must contain a number less than or equal to {param}.';
66-
$lang['form_validation_greater_than'] = 'The {field} field must contain a number greater than {param}.';
67-
$lang['form_validation_greater_than_equal_to'] = 'The {field} field must contain a number greater than or equal to {param}.';
68-
$lang['form_validation_error_message_not_set'] = 'Unable to access an error message corresponding to your field name {field}.';
69-
$lang['form_validation_in_list'] = 'The {field} field must be one of: {param}.';
40+
$lang['form_validation_required'] = 'The {field} field is required.';
41+
$lang['form_validation_isset'] = 'The {field} field must have a value.';
42+
$lang['form_validation_valid_email'] = 'The {field} field must contain a valid email address.';
43+
$lang['form_validation_valid_emails'] = 'The {field} field must contain all valid email addresses.';
44+
$lang['form_validation_valid_url'] = 'The {field} field must contain a valid URL.';
45+
$lang['form_validation_valid_ip'] = 'The {field} field must contain a valid IP.';
46+
$lang['form_validation_valid_base64'] = 'The {field} field must contain a valid Base64 string.';
47+
$lang['form_validation_min_length'] = 'The {field} field must be at least {param} characters in length.';
48+
$lang['form_validation_max_length'] = 'The {field} field cannot exceed {param} characters in length.';
49+
$lang['form_validation_exact_length'] = 'The {field} field must be exactly {param} characters in length.';
50+
$lang['form_validation_alpha'] = 'The {field} field may only contain alphabetical characters.';
51+
$lang['form_validation_alpha_numeric'] = 'The {field} field may only contain alpha-numeric characters.';
52+
$lang['form_validation_alpha_numeric_spaces'] = 'The {field} field may only contain alpha-numeric characters and spaces.';
53+
$lang['form_validation_alpha_dash'] = 'The {field} field may only contain alpha-numeric characters, underscores, and dashes.';
54+
$lang['form_validation_numeric'] = 'The {field} field must contain only numbers.';
55+
$lang['form_validation_is_numeric'] = 'The {field} field must contain only numeric characters.';
56+
$lang['form_validation_integer'] = 'The {field} field must contain an integer.';
57+
$lang['form_validation_regex_match'] = 'The {field} field is not in the correct format.';
58+
$lang['form_validation_matches'] = 'The {field} field does not match the {param} field.';
59+
$lang['form_validation_differs'] = 'The {field} field must differ from the {param} field.';
60+
$lang['form_validation_is_unique'] = 'The {field} field must contain a unique value.';
61+
$lang['form_validation_exists'] = 'The {field} field exists already.';
62+
$lang['form_validation_is_natural'] = 'The {field} field must only contain digits.';
63+
$lang['form_validation_is_natural_no_zero'] = 'The {field} field must only contain digits and must be greater than zero.';
64+
$lang['form_validation_decimal'] = 'The {field} field must contain a decimal number.';
65+
$lang['form_validation_less_than'] = 'The {field} field must contain a number less than {param}.';
66+
$lang['form_validation_less_than_equal_to'] = 'The {field} field must contain a number less than or equal to {param}.';
67+
$lang['form_validation_greater_than'] = 'The {field} field must contain a number greater than {param}.';
68+
$lang['form_validation_greater_than_equal_to'] = 'The {field} field must contain a number greater than or equal to {param}.';
69+
$lang['form_validation_error_message_not_set'] = 'Unable to access an error message corresponding to your field name {field}.';
70+
$lang['form_validation_in_list'] = 'The {field} field must be one of: {param}.';
7071
$lang['form_validation_honey_check'] = 'The {field} field must be filled with style.';
7172
$lang['form_validation_honey_time'] = 'The {field} field may only be needed once.';
73+
$lang['form_validation_is_exactly'] = "The {field} field does not contain expected values.";
74+
$lang['form_validation_is_not'] = "The {field} field does not accept entered value.";
75+
$lang['form_validation_valid_json'] = "The {field} field must be a valid json.";
76+
$lang['form_validation_valid_hour'] = "The {field} field must be a valid hour.";
77+
$lang['form_validation_valid_date'] = "The {field} field must be a valid date.";
78+
$lang['form_validation_valid_range_date'] = "The {field} field must be a valid date range.";
79+
$lang['form_validation_valid_latitude'] = "The %s latitude doesn't have a correct position.";
80+
$lang['form_validation_valid_longitude'] = "The %s longitude doesn't have a correct position.";
81+
$lang['form_validation_valid_latlong'] = "The %s map coodinates doesn't have a correct position.";
7282

7383
// --------------------------------------------------------------------
7484

75-
$lang['file_required'] = 'The field {field} is required.';
76-
$lang['file_size_max'] = "The file uploaded in {field} is too big (max size is {param}).";
77-
$lang['file_size_min'] = "The file uploaded in {field} is too small (min size is {param}).";
78-
$lang['file_allowed_type'] = "The file uploaded in {field} should be {param}.";
79-
$lang['file_disallowed_type'] = "The file uploaded in {field} cannot be {param}.";
80-
$lang['file_image_maxdim'] = "The dimensions of the file uploaded in {field} are too big.";
81-
$lang['file_image_mindim'] = "The dimensions of the file uploaded in {field} are too small.";
82-
$lang['file_image_exactdim'] = "The file uploaded in {field} doesn't have the right dimensions.";
83-
$lang['is_exactly'] = "The field {field} contains a non valid value.";
84-
$lang['is_not'] = "The field {field} contains a non valid value.";
85-
$lang['error_max_filesize_phpini'] = "The file exceeds the upload_max_filesize directive from php.ini.";
86-
$lang['error_max_filesize_form'] = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.";
87-
$lang['error_partial_upload'] = "The file it's only partially uploaded.";
88-
$lang['error_temp_dir'] = "Temp dir error.";
85+
$lang['file_required'] = 'The {field} field needs a file to upload.';
86+
$lang['file_max_size'] = "The {field} file is too big (max size is {param}).";
87+
$lang['file_min_size'] = "The {field} file is too small (min size is {param}).";
88+
$lang['file_allowed_type'] = "The {field} file allowed should be a/an {param}.";
89+
$lang['file_disallowed_type'] = "The {field} file type {param}, is not allowed.";
90+
$lang['file_image_maxdim'] = "The dimensions of the {field} file are too big.";
91+
$lang['file_image_mindim'] = "The dimensions of the {field} file are too small.";
92+
$lang['file_image_exactdim'] = "The {field} file doesn't have the right dimensions.";
93+
$lang['error_max_filesize_phpini'] = "The uploaded file exceeds the maximum upload size of PHP."; // from php.ini
94+
$lang['error_max_filesize_form'] = "The uploaded file exceeds the MAX_FILE_SIZE allowed."; // from form validation
95+
$lang['error_partial_upload'] = "The file is only partially uploaded.";
96+
$lang['error_temp_dir'] = "Temp directory error.";
8997
$lang['error_disk_write'] = "Disk write error.";
9098
$lang['error_stopped'] = "File upload stopped by extension";
9199
$lang['error_unexpected'] = "Unexpected file upload error. Error: {field}";
92-
$lang['valid_json'] = "The field {field} must be a valid json.";
93-
$lang['valid_hour'] = "The field {field} must be a valid hour.";
94-
$lang['valid_date'] = "The field {field} must be a valid date.";
95-
$lang['valid_range_date'] = "The field {field} must be a valid date range.";
96-
$lang['valid_latitude'] = "The %s latitude doesn't have a correct position.";
97-
$lang['valid_longitude'] = "The %s longitude doesn't have a correct position.";
98-
$lang['valid_latlong'] = "The %s map coodinates doesn't have a correct position.";
100+
$lang['honey_check'] = 'The {field} field must be filled with style.';
101+
$lang['honey_time'] = 'The {field} field may only be needed once.';

framework/libraries/Form_validation.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,38 @@ public function set_rules($field, $label = '', $rules = [], $errors = [])
244244
return $this;
245245
}
246246

247+
/**
248+
* Alias to the above method
249+
*
250+
* This function takes an array of field names and validation
251+
* rules as input, any custom error messages, validates the info,
252+
* and stores it
253+
*
254+
* @param mixed $field
255+
* @param string $label
256+
* @param mixed $rules
257+
* @param array $errors
258+
* @return CI_Form_validation
259+
*/
260+
public function rules($field, $label = '', $rules = [], $errors = [])
261+
{
262+
return $this->set_data($field, $label, $rules, $errors);
263+
}
264+
265+
/**
266+
* Alias to the above method
267+
*
268+
* @param mixed $field
269+
* @param string $label
270+
* @param array $rules
271+
* @param array $errors
272+
* @return CI_Form_validation
273+
*/
274+
public function rule($field, $label = '', $rules = [], $errors = [])
275+
{
276+
return $this->rules($field, $label, $rules, $errors);
277+
}
278+
247279
// --------------------------------------------------------------------
248280

249281
/**
@@ -303,6 +335,21 @@ public function set_message($lang, $val = '')
303335
return $this;
304336
}
305337

338+
/**
339+
* Alias to the method above
340+
*
341+
* Lets users set their own error messages on the fly. Note:
342+
* The key name has to match the function name that it corresponds to.
343+
*
344+
* @param array
345+
* @param string
346+
* @return CI_Form_validation
347+
*/
348+
public function setMessage($lang, $val = '')
349+
{
350+
return $this->set_message($lang, $val);
351+
}
352+
306353
// --------------------------------------------------------------------
307354

308355
/**
@@ -492,6 +539,19 @@ public function run($group = '')
492539
return ($total_errors === 0);
493540
}
494541

542+
/**
543+
* Alias to the method above
544+
*
545+
* This function does all the work.
546+
*
547+
* @param string $group
548+
* @return bool
549+
*/
550+
public function check($group = '')
551+
{
552+
return $this->run($group);
553+
}
554+
495555
// --------------------------------------------------------------------
496556

497557
/**

0 commit comments

Comments
 (0)