Describe the bug
the error message thrown by Yup for an email array field does not show up in ErrorMessage.
To Reproduce
Codesandbox link
https://codesandbox.io/s/kind-lamport-vopi4
Expected behavior
error.message
being passed to ErrorMessage and displaying.
Screenshots
As shown above, the message object passed by ErrorMessage
for Recipients shows both Message and Messages property as undefined. Hence, no error is displayed.
message
isn't passed down from ValidationError
to the errors
object.
Desktop (please complete the following information):
Additional context
Prior to validating recipientList
as an Array of Emails, I applied a transform
to comma delimited string from the Recipients input into an array of emails to match against.
const surveyFormSchema = object().shape({
subject: string().required(),
body: string().required(),
recipientList: array()
// This transforms a comma delimited string into an array of emails
// and then performs email validation on each one.
.transform(function(value, originalValue) {
if (this.isType(value) && value !== null) {
return value;
}
return originalValue ? originalValue.split(/[\s,]+/) : [];
})
.required("At least one email is required")
.of(string().email(({ value }) => `${value} is an invalid email.`))
});
After console logging, the errors
object from useForm
, I see that errors[recipientList]
is an Array of 1 instead of being an object like errors[subject]
and errors[body]
.
errors[recipientList][0]
does give back an object with the right message.
Any idea why it becomes an Array instead of staying as an Object?
recipientList: array()
// This transforms a comma delimited string into an array of emails
// and then performs email validation on each one.
.transform(function(value, originalValue) {
if (this.isType(value) && value !== null) {
return value;
}
return originalValue ? originalValue.split(/[\s,]+/) : [];
})
.required("At least one email is required")
.of(string().email(({ value }) => `${value} is an invalid email.`))
because you r return array right?
Ah, I see why now.
To answer my own question, I solved it by checking for whether error[name] isArray()
, then map it to a list of p
elements
<input name={name} ref={register} />
{error[name] && error[name].map(({message}) => {
return <p>{message}</p>
})}
_should probably wrap the above in ul
and li
(provide keys as well)._
To not take too much space, I instead returned the value
in the yup schema
const schema = object.shape({
...
list: array().of(string().email(({value}) => value)
})
then join
the error'ed entries into a single string
const customErrorMessage = () => errors[name].map(({message}) => message).join(', ') + ' are invalid emails.'
...
return (
<>
<input name={name} ref={register} />
{errors[name] && <p>{customErrorMessage()}</p>
</>
)
awesome glad you work it out :)
Most helpful comment
Ah, I see why now.
To answer my own question, I solved it by checking for whether error[name]
isArray()
, then map it to a list ofp
elements_should probably wrap the above in
ul
andli
(provide keys as well)._To not take too much space, I instead returned the
value
in the yup schemathen
join
the error'ed entries into a single string