Codeigniter4: Bug: Most validation rules enforce requirement of the field

Created on 20 May 2020  ·  3Comments  ·  Source: codeigniter4/CodeIgniter4

When using predefined validation rules like alpha, decimal, greater_than and less_than the field can't be optional, since these rules still kick in, even if the field is empty.

Expected behavior and Suggestion
Most of the predefined rules should pass trough if the field is NULL. Then use required to check for empty fields.
There might be issues with 0 numbers / decimals and required (required does not only check NULL - empty(trim($str))) but we could prevent these in custom or more specific rules instead of defining thousands of new rules like decimal_optional.

But we can't check if the Field is NULL before applying rules, because we want to be able to validate against NULL in some rules like required or maybe custom rules. So we have to check for NULL in every rule separately.

Steps to reproduce
Set alpha as validation rule, let the field blank and try to validate. In my opinion the validation error should only occur when using alpha and required together.

Quickfix
As stated in the comments, use the permit_empty rule.

CodeIgniter 4 version
Released 4.0.3

Affected module(s)
Validation\{FormatRules, FileRules} (at least)

bug

Most helpful comment

Just use permit_empty rule.

All 3 comments

Just use permit_empty rule.

This does work, thanks for the quick reply.

Anyway i see the described behavior as a problem (bug or feature 🤷‍♂️), at least it should get discussed.

This behavior was intended.

At this point, any change to the rules that you mentioned would be a BC change, so this will not happen.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Martin-4Spaces picture Martin-4Spaces  ·  3Comments

Iamscalla picture Iamscalla  ·  4Comments

lonnieezell picture lonnieezell  ·  6Comments

MisterAnmar picture MisterAnmar  ·  5Comments

petk picture petk  ·  4Comments