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

$error is true even though required is false #1212

Closed
iulianwebdev opened this issue Oct 2, 2023 · 3 comments
Closed

$error is true even though required is false #1212

iulianwebdev opened this issue Oct 2, 2023 · 3 comments

Comments

@iulianwebdev
Copy link

iulianwebdev commented Oct 2, 2023

Make sure that you are familiar with documentation before submitting an issue.

https://vuelidate.netlify.com/

Describe the bug
Hi, so I have a dynamic required property on the validation. It has to be optional, so it depends on an external model value
But when the value is empty and required: false, $error seems to be set to true. This messes up the form error highlighting in my projects
So 2 cases are wrong:

  1. When $model: "" and required: false ($error will show as true)
    1.a Also when $model is not "", required will turn to true? Why is that?
  2. When $model:"" and required: true ($error will show as false although it should be true, cause it is required)

Reproduction URL
I have a small sandbox here:
https://jsfiddle.net/2seokxmf/1/

Vuelidate Template

Issues without a working fiddle are generally much harder to solve and usually take much more time to actually do it.

To Reproduce
Steps to reproduce the behavior:
Type in the textbox and toggle the checkbox

@iulianwebdev
Copy link
Author

Any hint on how can I proceed with this?

@irsyaadbp
Copy link

irsyaadbp commented Dec 27, 2023

Change with this, because if you want to custom validation, it must validate the value, not the this.tick

validations() {
  return {
    text: {
    	required: function (value) {
          if(this.tick) return !!value
          return true
      }
    }
  }
},

Or if you want to add condition with required, you can use required if

validations() {
  return {
    text: {
    	required: requiredIf(() => this.tick)
    }
  }
},

@iulianwebdev
Copy link
Author

This was it. I was missing the requiredIf function. Thank you for taking time and clarifying this for me.

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

No branches or pull requests

2 participants