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

validate one field based on another field value #405

Closed
bhargavavarma opened this issue May 25, 2022 · 7 comments
Closed

validate one field based on another field value #405

bhargavavarma opened this issue May 25, 2022 · 7 comments

Comments

@bhargavavarma
Copy link

Describe the bug

We have 2 select fields named select_field1 and select_field2 and both are dependent on each other. when select_field1 value is changing, the select_field2 should also be validated and show error message based on validate function. We are passing select_field1 and its value to useField of select_field2, select_field2 component is re rendering when select_field1 value changes, but the select_field2 error value is not getting set.

@joepuzzo
Copy link
Collaborator

@joepuzzo
Copy link
Collaborator

you should not be trying to use useField or useFieldState at this point. Create your custom inputs once with useField and then you should never be touching that hook again.

@joepuzzo
Copy link
Collaborator

joepuzzo commented May 25, 2022

<Input
    name="password"
    id="notify-password"
    validate={validatePassword}
    validateOnChange
    validateWhen={['confirmPassword']}
  />
  <Input
    name="confirmPassword"
    id="notify-confirm-password"
    validate={validateConfim}
    validateOnChange
    validateWhen={['password']}
  />
 <button type="submit">Submit</button>
</Form>;

@bhargavavarma
Copy link
Author

Are you using paired validation ??? https://teslamotors.github.io/informed/?path=/story/validation--paired-validation

@joepuzzo
Both the select fields will have same validate function. In validate function we compare the user selected start date and end date values, if startDate > endDate will return an error.

We have used validateWhen, but also we couldn't achieve the desired output.

@joepuzzo
Copy link
Collaborator

did you do exactly as the docs show? Can you recreate issue in a code sandbox https://teslamotors.github.io/informed/?path=/story/playground--format-example

@joepuzzo
Copy link
Collaborator

https://codesandbox.io/s/validatepaireddate-71b7pt?file=/App.js

See this example working ^^^^

@bhargavavarma
Copy link
Author

https://codesandbox.io/s/validatepaireddate-71b7pt?file=/App.js

See this example working ^^^^

its working. Thankyou @joepuzzo

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

2 participants