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.

How to use requiredIf?

See original GitHub issue

First question

Probably I’ve not understood a basic thing: must requiredIf return true to indicate that the field itself is required, or true if the validation is successfully passed?

for example: when the password is not mandatory, must my function return false (because is not mandatory), or true (because the ‘require’ validation has been passed) ?!?!

Second question

What is supposed to be the param of requiredIf?

I’ve a form used both for creating new users and for editing existing users.

If creating, the computed method isNewUser returns true.

If editing, it returns false,

I’m trying to implement the rule that password is mandatory only if isNewUser.


computed: {
        isNewUser() {
            return this.$store.getters["customers/getIsNewUser"];
        }
},
validations: {
    ....
    new_password: {
                minLength: minLength(8),
                sameAs: sameAs("repeat_new_password"),
                required: requiredIf(function(model) {

                    console.log("isNewUser", this.isNewUser);
                    console.log("model", model);
                    console.log("model.new_password", model.new_password);
                    const esit =
                        !this.isNewUser ||
                        (this.isNewUser && model.new_password);
                    console.log("esit", esit);
                    return esit;
                })
    }
   ...
}

This is my console output when inserting a new user.

image

What kind of what is the model param?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

26reactions
dobromir-hristovcommented, Jun 10, 2019
1reaction
muarachmanncommented, Mar 2, 2021

Hi @dobromir-hristov thanks for the fiddle, i have been playing with vuelidate and its awesome. I however have a blocker… what if the field in question needs to be validated by another field of the model. I have this validation

                tracks: {
                    $each: {
                        title: {
                            required,
                            minLength: minLength(3)
                        },
                        price: {
                            required,
                            minValue: minValue(100)
                        },
                        gold_price: {
                             required: requiredIf(function() {
                             return ..... -> what to put here, 
                        })
                        },
                    }
                }

Basically in my object model track it has a prop called is_gold_price which is a check button that unhides and hides the gold price field. I wish to be able to access each instance of the is_gold_price, So the field is to be required if an only if the is_gold_price has been toggled on which is for some particular track. How can one access the props of each? A little help would be appreciated thanks

Read more comments on GitHub >

github_iconTop Results From Across the Web

asp.net mvc 3 - RequiredIf Conditional Validation Attribute
Tips To Get The Javascript Code Part Work · 1- put the code in a new js file (ex:requiredIfValidator.js) · 2- warp the...
Read more >
Required If Validation - MSDN - Microsoft
I would like UploadFile to be required if @Model.Filename is null . Of course I can make it required all the time in...
Read more >
NET 6.0 Blazor custom validation - YouTube
.NET 6.0 Blazor custom validation | RequiredIf validation in Blazor.
Read more >
[Solved] How to use data annotation to check for two values.
I am using .NET Core for the solution and have seen exampled for RequireIF but it only checks one field. I need multiple...
Read more >
MVC 3 RequiredIf validator for multiple values
... using DataAttributes... Problem for me: I needed the field to be Required if the selected value in my dropdownlist was either 1,...
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