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 user allergy input #3573
Validate user allergy input #3573
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
<Field | ||
label="Matallergier/preferanser" | ||
parse={(value) => value} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain why this is necessary? I don't doubt that it is, but does seem a bit weird.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When updating your user settings, you perform a PATCH
request, and react-final-form
therefore removes empty values. This means that if you remove your allergies from something to nothing, it won't be patched. This overrides their default parser, so that empty values are kept.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, this is good to know!
It's surprisingly common for people to write "ingen/nei".
0da9d75
to
e3499bc
Compare
message = 'La feltet stå tomt hvis du ikke har noen allergier/preferanser' | ||
) => | ||
(value: string) => { | ||
const notValidAnswers = ['ingen', 'ingenting', 'nei', 'nope', 'nada']; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we should also include "nay", "nah" and "negative" :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there any examples of people entering them in the allergies field?
(value: string) => { | ||
const notValidAnswers = ['ingen', 'ingenting', 'nei', 'nope', 'nada']; | ||
|
||
return [!notValidAnswers.includes(value.toLowerCase()), message]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain how this syntax works! It looks interesting :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure which part you're referring to. The validator needs an array with two values; a boolean and a message. The boolean indicates if the validation has passed or not, and the message is what is given to the user. What we're actually creating here is a function returning a function which returns the array.
Description
It's surprisingly common for people to write "ingen/nei".
Result
Testing
Tested with and without valid inputs.