Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
element-code opened this issue May 20, 2020 · 3 comments
Closed

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

element-code opened this issue May 20, 2020 · 3 comments
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@element-code
Copy link
Contributor

element-code commented May 20, 2020

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)

@element-code element-code added the bug Verified issues on the current code behavior or pull requests that will fix them label May 20, 2020
@michalsn
Copy link
Member

Just use permit_empty rule.

@element-code
Copy link
Contributor Author

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.

@michalsn
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

No branches or pull requests

2 participants