CodeIgniter Forums
How rules works? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: How rules works? (/showthread.php?tid=88340)



How rules works? - motoroller - 08-25-2023

please explain how its works if_exist?
What difference permit_empty?


RE: How rules works? - sammyskills - 08-25-2023

From the docs,

Quote:if_exists: If this rule is present, validation will check the field only when the field key exists in the data to validate.

More explanation:
Validation will only check the field when the field key is present in the request data to validate. For example, you set the rule on a field like so:
PHP Code:
$rule = ['username' => "if_exists|numeric"]; 
If you submit a form that doesn't contain the input/field name of username, validation will not check that field, irrespective of the rules that follow, in this case, numeric. But if your form contains the input/field name, then validation will check for other in that same field.

Quote:permit_empty: Allows the field to receive an empty array, empty string, null or false.

More explanation:
Validation will allow a field to accept an empty value, irrespective of the associated rules. For example, you set the rule on a field like so:
PHP Code:
$rule = ['username' => "permit_empty|numeric"]; 
Your form with input/field name of username can successfully pass validation checks if the field does not contain any value.