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

How to access data inside of custom validation #188

Closed
alanhr opened this issue Jun 1, 2017 · 4 comments
Closed

How to access data inside of custom validation #188

alanhr opened this issue Jun 1, 2017 · 4 comments
Assignees

Comments

@alanhr
Copy link
Contributor

alanhr commented Jun 1, 2017

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 ?

@mikeerickson mikeerickson self-assigned this Jun 1, 2017
pcraig3 added a commit to cds-snc/ircc-rescheduler that referenced this issue Sep 25, 2018
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
pcraig3 added a commit to cds-snc/ircc-rescheduler that referenced this issue Sep 25, 2018
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
@raiman264
Copy link

I noticed that you have access to input in the this object
for example:

validatorjs.register(
  'equals',
  function equalsRule(value, requirement) {
    return value === this.validator.input[requirement];
  },
   'The :attribute must be equals to :requirement.'
);

where requirement is a field name

@smithaitufe
Copy link

Hi @raiman264 Please how did you access the name of the second field? The :attribute property works but the :requirement does not

@ravanspell
Copy link

I noticed that you have access to input in the this object
for example:

validatorjs.register(
  'equals',
  function equalsRule(value, requirement) {
    return value === this.validator.input[requirement];
  },
   'The :attribute must be equals to :requirement.'
);

where requirement is a field name

What if we don't have access to "this" instance?

@ravanspell
Copy link

Hi @raiman264 Please how did you access the name of the second field? The :attribute property works but the :requirement does not

In your case it's easy
validation: 'min:3|formula-format: 3'

add your field name as the second param. for instance, above assume formula-format is a custom validation rule.
you can insert the field name you intend to access as '3' in the above example. then you will get "requirement" = 3 in this case.

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

No branches or pull requests

5 participants