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.

Conditional built-in validators

See original GitHub issue

Hi there, I’m looking for a super basic conditional validation using built-in validators, but can’t seem to figure out how to accomplish it with vuelidate.

In essence:

fixedFee: {
  required: requiredIf('hasFixedFee'),
  minValue: minValue(1),
},

Problem is that minValue validator always triggers, even if fixedFee is empty or not required. How can I use the built-in minValue validator but make it conditional to hasFixedFee like requiredIf?

Having to supply my own validation function for such a simple use case seems overly complicated to me.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:15 (7 by maintainers)

github_iconTop GitHub Comments

22reactions
dobromir-hristovcommented, Jun 10, 2019

You could create such a helper.

Here is an example - https://jsfiddle.net/dobromir/d20bL4oj/10/

Helper

const validateIf = (prop, validator) =>
  helpers.withParams({ type: 'validatedIf', prop }, function(value, parentVm) {
    return helpers.ref(prop, this, parentVm) ? validator(value) : true
  })

Usage

validations: {
    form: {
      password: {
      	minLength: validateIf('change_password', minLength(5))
      }
    }
  }

If anyone finds this useful, please vote or something, I could add it as a helper 😃

7reactions
skajfescommented, Apr 8, 2020

I have solved a similar problem using dynamic validation schema, and it seems to me this is closely related. The docs are here: https://vuelidate.js.org/#sub-dynamic-validation-schema

My problem was having to validate a different set of fields based on selection. Ie, based on paymentType selection validate the appropriate input fields:

    validations() {
        let v = {
            // validators that are always applied
            
        };

        // different set of validators based on selection
        if (this.paymentType === 'credit-card') {
            v.creditCardNumber = {required};
        } 
        else if (this.paymentType === 'invoice') {
            v.companyName = {required};
            v.companySSN = {required};
        }
        return v;

Applied to the OP’s problem it would look like:

validations() {
     var v = {  };
    
     if (this.hasFixedFee) {
          v.fixedFee = { required, minValue: minValue(1) };
     }

     return v;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Conditional Validation in Reactive Forms - Codementor
Validating your application form before reaching out to the server enhances security and performance.
Read more >
Conditional Vuelidate Validator - Stack Overflow
i want to have a conditional validator that checks for (in this case) minValue(0) or minValue(0.01), depending on other things on the page....
Read more >
Angular Conditional Validation - ConcretePage.com
To perform conditional validations with formControlName , formControl and ngModel using built-in validators, we can use build-in validator ...
Read more >
Reactive Forms Benefits: Conditional Validation - Preston Lamb
If the value is false , we clear all the validators. But the last part is very important as well.
Read more >
Active Record Validations - Ruby on Rails Guides
5 Conditional Validation ... Sometimes it will make sense to validate an object only when a given predicate is satisfied. You can do...
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