*The Chakra docs describe how to set isValid=false
and display the FormErrorMessage.
However, afaics, one of the downsides of their implementation is that the errorBorderColor
is displayed when the form loads. To work around this I added an onClick
function to the Submit button. (Code below for anyone who’s interested. If there are better ways to do this, would love to hear.)
Re: the some of the examples above, it looks as though the client & server side message fields are being conflated. Linking to the redwood docs for clarification.
<FormError>
Displays error messages from the server. Typically placed at the top of your form
<FieldError>
Displays error messages if the field with the samename
prop has validation errors. Only renders if there’s an error on the associated field
The following now works well for me for client side validations:
const [emailError, setEmailError] = useBoolean(false)
const [emailValue, setEmailValue] = useState('')
const handleInputChange = (e) => setEmailValue(e.target.value)
const onSubmit = (data: FormEvent) => {
props.onSave(data, props?.event?.id)
}
const onClick = () => {
if (emailValue === '') setEmailError.toggle()
}
...
<FormControl isInvalid={emailError}>
<FormLabel htmlFor="email">Email address</FormLabel>
<Input
as={TextField}
name="email"
value={emailValue}
focusBorderColor={eventColorScheme}
errorBorderColor={
emailValue === '' && emailError ? 'red.300' : 'inherit'
}
placeholder="Email address"
onChange={handleInputChange}
validation={{
required: {
value: true,
message: 'Email is required',
},
}}
/>
{!emailError ? (
<FormHelperText>Some help text when no error</FormHelperText>
) : (
<FormErrorMessage as={FieldError} name="email" />
)}
</FormControl>
<Flex justify="space-between" mt="10">
<Spacer />
<Button colorScheme={eventColorScheme}>
<Submit disabled={props.loading} onClick={onClick}>
Submit
</Submit>
</Button>
</Flex>
I have an action to no-longer wrap Submit
in Button
but it works for now.