Just finished the Form building part of the tutorial, and I found it very repetitive on the error styles part.
I wonder whether Redwood could add the option to have a “global” errorStyle
and/or errorClass
declared at the <Form>
element. It would automatically apply to all nested input elements and their labels for instance, and any errorStyle
or errorClass
declared at an input/label level would override the Form one (maybe merge styles & override class name).
The tutorial form would then go from this:
<Form onSubmit={onSubmit} validation={{ mode: 'onBlur' }}>
<Label
name="name"
style={{ display: 'block' }}
errorStyle={{ display: 'block', color: 'red' }}
>
Name
</Label>
<TextField
name="name"
validation={{ required: true }}
errorStyle={{ display: 'block', borderColor: 'red' }}
/>
<FieldError name="name" style={{ color: 'red' }} />
<Label
name="email"
style={{ display: 'block' }}
errorStyle={{ display: 'block', color: 'red' }}
>
Email
</Label>
<TextField
name="email"
validation={{
required: true,
pattern: {
value: /[^@]+@[^\.]+\..+/,
message: 'Please enter a valid email address',
},
}}
errorStyle={{ display: 'block', borderColor: 'red' }}
/>
<FieldError name="email" style={{ color: 'red' }} />
<Label name="message" style={{ display: 'block' }}>
Message
</Label>
<TextAreaField
name="message"
validation={{ required: true }}
errorStyle={{ display: 'block', borderColor: 'red' }}
/>
<FieldError name="message" style={{ color: 'red' }} />
<Submit style={{ display: 'block' }}>Send</Submit>
</Form>
to:
<Form
onSubmit={onSubmit}
validation={{ mode: 'onBlur' }}
errorStyle={{ display: 'block', borderColor: 'red', color: 'red' }}
>
<Label name="name" style={{ display: 'block' }}>
Name
</Label>
<TextField name="name" validation={{ required: true }} />
<FieldError name="name" style={{ color: 'red' }} />
<Label name="email" style={{ display: 'block' }}>
Email
</Label>
<TextField
name="email"
validation={{
required: true,
pattern: {
value: /[^@]+@[^\.]+\..+/,
message: 'Please enter a valid email address',
},
}}
/>
<FieldError name="email" style={{ color: 'red' }} />
<Label name="message" style={{ display: 'block' }}>
Message
</Label>
<TextAreaField name="message" validation={{ required: true }} />
<FieldError name="message" style={{ color: 'red' }} />
<Submit style={{ display: 'block' }}>Send</Submit>
</Form>
More realistically, errorClass
would more likely be used in that kind of use case, so that different styles for different types of inputs/labels would be covered with a single class.
Not sure that’s the API to go for anyway, but what are your thoughts on the idea?