-
-
Notifications
You must be signed in to change notification settings - Fork 279
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
How to access data inside of custom validation #188
Comments
Here was the problem: - we want availability to be set to the last thing we set it to - (so we should keep it in the app context) - but we can't save `availability` without the validation running - but validation fails because selectedDays always fails if it's not set or whatever So: - add availability to CalendarFields and save anything But now what we need is a validation rule that requires selectedDays _unless_ availability exists. If availability exists, then we don't need to validate selectedDays. Problems: - `required_unless` will only work with a non-array value because of how validatorjs is written and how JavaScript works - (ie, `['hello'] === ['hello']` is false in JS) - https://github.com/skaterdav85/validatorjs#required_unlessanotherfieldvalue - we can't define a custom rule that knows about other values - mikeerickson/validatorjs#188 - mikeerickson/validatorjs#287 But that last person gave us a workaround: > Looks like the functionality you requesting is not in place. At least I also do not know the solution. > In my case, I analyzed values first, and then dynamically create a rules object. Run validation afterward. So that's what we are doing here. If values.availability exists, override the validation rule for selectedDays (but don't overwrite the original object). Else, set it to the original rules. This solves both of our problems from earlier: - existing selected dates will always be saved - whatever the user last clicked in the checkbox, that will be saved as well
Here was the problem: - we want availability to be set to the last thing we set it to - (so we should keep it in the app context) - but we can't save `availability` without the validation running - but validation fails because selectedDays always fails if it's not set or whatever So: - add availability to CalendarFields and save anything But now what we need is a validation rule that requires selectedDays _unless_ availability exists. If availability exists, then we don't need to validate selectedDays. Problems: - `required_unless` will only work with a non-array value because of how validatorjs is written and how JavaScript works - (ie, `['hello'] === ['hello']` is false in JS) - https://github.com/skaterdav85/validatorjs#required_unlessanotherfieldvalue - we can't define a custom rule that knows about other values - mikeerickson/validatorjs#188 - mikeerickson/validatorjs#287 But that last person gave us a workaround: > Looks like the functionality you requesting is not in place. At least I also do not know the solution. > In my case, I analyzed values first, and then dynamically create a rules object. Run validation afterward. So that's what we are doing here. If values.availability exists, override the validation rule for selectedDays (but don't overwrite the original object). Else, set it to the original rules. This solves both of our problems from earlier: - existing selected dates will always be saved - whatever the user last clicked in the checkbox, that will be saved as well
I noticed that you have access to
where requirement is a field name |
Hi @raiman264 Please how did you access the name of the second field? The |
What if we don't have access to "this" instance? |
In your case it's easy add your field name as the second param. for instance, above assume |
Hi,
I need access the value of other field inside of the data equal a required_if, but in callback I don't have access this values.
Why not pass the input as the last param ?
The text was updated successfully, but these errors were encountered: