Skip to content

Bug: Most validation rules enforce requirement of the field #3025

Closed
@element-code

Description

@element-code

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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugVerified issues on the current code behavior or pull requests that will fix them

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions