added ability to add in custom validation error messages for text input fields w/ burnettk

This commit is contained in:
jasquat 2023-01-13 16:55:34 -05:00
parent 419f23490d
commit 708c2722d8
6 changed files with 87 additions and 22 deletions

View File

@ -228,6 +228,10 @@ h1.with-icons {
background: rgba(0,0,0,.05); background: rgba(0,0,0,.05);
} }
.form-instructions {
margin-bottom: 10em;
}
/* Json Web Form CSS Fix - Bootstrap now requries that each li have a "list-inline-item." Also have a PR /* Json Web Form CSS Fix - Bootstrap now requries that each li have a "list-inline-item." Also have a PR
in on this with the react-jsonschema-form repo. This is just a patch fix to allow date inputs to layout a little more cleanly */ in on this with the react-jsonschema-form repo. This is just a patch fix to allow date inputs to layout a little more cleanly */
.list-inline>li { .list-inline>li {

View File

@ -89,7 +89,11 @@ export default function BaseInputTemplate<
let errorMessageForField = null; let errorMessageForField = null;
if (rawErrors && rawErrors.length > 0) { if (rawErrors && rawErrors.length > 0) {
invalid = true; invalid = true;
errorMessageForField = `${labelToUse.replace(/\*$/, '')} ${rawErrors[0]}`; if ('validationErrorMessage' in schema) {
errorMessageForField = (schema as any).validationErrorMessage;
} else {
errorMessageForField = `${labelToUse.replace(/\*$/, '')} ${rawErrors[0]}`;
}
} }
return ( return (
@ -97,6 +101,7 @@ export default function BaseInputTemplate<
<TextInput <TextInput
id={id} id={id}
name={id} name={id}
className="input"
labelText={labelToUse} labelText={labelToUse}
invalid={invalid} invalid={invalid}
invalidText={errorMessageForField} invalidText={errorMessageForField}

View File

@ -1,17 +1,34 @@
import React from 'react'; import React from 'react';
import Typography from '@mui/material/Typography'; import {
import { DescriptionFieldProps } from '@rjsf/utils'; DescriptionFieldProps,
FormContextType,
RJSFSchema,
StrictRJSFSchema,
} from '@rjsf/utils';
function DescriptionField({ description, id }: DescriptionFieldProps) { /** The `DescriptionField` is the template to use to render the description of a field
if (description) { *
* @param props - The `DescriptionFieldProps` for this component
*/
export default function DescriptionField<
T = any,
S extends StrictRJSFSchema = RJSFSchema,
F extends FormContextType = any
>(props: DescriptionFieldProps<T, S, F>) {
const { id, description } = props;
if (!description) {
return null;
}
if (typeof description === 'string') {
return ( return (
<Typography id={id} variant="subtitle2" style={{ marginTop: '5px' }}> <p id={id} className="field-description">
{description} {description}
</Typography> </p>
); );
} }
return (
return null; <div id={id} className="field-description">
{description}
</div>
);
} }
export default DescriptionField;

View File

@ -0,0 +1,9 @@
### Custom Validation Error Message for String Inputs
If you have a property in your json schema like:
"user_generated_number_1": {"type": "string", "title": "User Generated Number", "default": "0", "minLength": 3}
it will generate this error message by default: "User Generated Number must NOT have fewer than 3 characters."
If you add the `validationErrorMessage` key to the property json it will print that message instead.

View File

@ -1,16 +1,27 @@
import React from 'react'; import React from 'react';
import Box from '@mui/material/Box'; import {
import Divider from '@mui/material/Divider'; FormContextType,
import Typography from '@mui/material/Typography'; TitleFieldProps,
import { TitleFieldProps } from '@rjsf/utils'; RJSFSchema,
StrictRJSFSchema,
} from '@rjsf/utils';
function TitleField({ id, title }: TitleFieldProps) { const REQUIRED_FIELD_SYMBOL = '*';
/** The `TitleField` is the template to use to render the title of a field
*
* @param props - The `TitleFieldProps` for this component
*/
export default function TitleField<
T = any,
S extends StrictRJSFSchema = RJSFSchema,
F extends FormContextType = any
>(props: TitleFieldProps<T, S, F>) {
const { id, title, required } = props;
return ( return (
<Box id={id} mb={1} mt={1}> <legend id={id} className="header">
<Typography variant="h5">{title}</Typography> {title}
<Divider /> {required && <span className="required">{REQUIRED_FIELD_SYMBOL}</span>}
</Box> </legend>
); );
} }
export default TitleField;

View File

@ -1,3 +1,22 @@
button.react-json-schema-form-submit-button { button.react-json-schema-form-submit-button {
margin-top: 1.5em; margin-top: 1.5em;
} }
.rjsf .header {
font-weight: 400;
font-size: 20px;
line-height: 28px;
color: #161616;
}
.rjsf .field-description {
font-size: 14px;
line-height: 18px;
letter-spacing: 0.16px;
color: #525252;
margin-bottom: 1em;
}
.rjsf .input {
margin-bottom: 2em;
}