question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

required_with and required_if rules cannot handle 0 value

See original GitHub issue

Sample code : method: 'numeric', description: 'required_with:method|required_if:method,0'

Validations are not triggered when method value is 0.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mikeericksoncommented, Jan 1, 2020

I can see the point here. Let me stew on this for a bit. My initial thinking is, if the value is required, but an empty string is supplied, theoretically that should fail validation.

Now, on the bad side, if I make this change, then I risk breaking other applications which don’t adopt this strictness

1reaction
codeburycommented, Dec 31, 2019

Hi.

The issue comes from the fact that required_if rule takes it’s parameter as string, so what it’s doing is: 0 === '0'.

As a workaround, I used the registerImplicit method provided by Validator class:

const required_if_rule = function (val, req, attribute) {
        req = this.getParameters();
        let op = this.validator._objectPath(this.validator.input, req[0]);        
        if ((op != undefined && op != null ? op.toString() : null) === req[1]) {
            return this.validator.getRule('required').validate(val);
        }
        return true;
    }

Validator.registerImplicit('required_if', required_if_rule, 'The :attribute field is required when :other is :value');
Read more comments on GitHub >

github_iconTop Results From Across the Web

Required if greater than 0 not working in laravel
I required one field if another field value is greater than 0. I tried like this 'scope' => 'required_if:project,>,1',. This ...
Read more >
Validation - Laravel - The PHP Framework For Web Artisans
The Validator class provides several rules for validating files, such as size , mimes , and others. When validating files, you may simply...
Read more >
Laravel 9 validation requiredIf and string rules
Hi everyone, I don't know why in laravel 9 the requiredIf rule don't take effect, ... @Sinnbeck a field name, its value can...
Read more >
Validation - Laravel guide
Available Validation Rules; Conditionally Adding Rules; Validating Arrays ... Next, let's take a look at a simple controller that handles these routes.
Read more >
RequiredAttribute Class (System.ComponentModel. ...
Specifies that a data field value is required. ... RequiredAttribute attribute to override the database schema rule that allows a data field to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found