Skip to content

Commit 3782f2d

Browse files
authored
Add validateOnLoad to FormField (#202)
Add validateOnLoad to FormField
2 parents 11bf2dd + 1bbf231 commit 3782f2d

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

packages/react-components/source/react/library/form/FormField.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,24 @@ const propTypes = {
1818
label: PropTypes.string.isRequired,
1919
/** The styling of the identifier for this field */
2020
labelType: PropTypes.oneOf(['primary', 'secondary']),
21-
/* Depending on the field, value can be any type */
21+
/** Depending on the field, value can be any type */
2222
// eslint-disable-next-line react/forbid-prop-types
2323
value: PropTypes.any,
2424
/** Form error, causing element to render red when present */
2525
error: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
2626
/** Expanded explainer for the field */
2727
description: PropTypes.string,
28-
/* Is the field required */
28+
/** Is the field required */
2929
required: PropTypes.bool,
30-
/* The error message to display if the field is required but not present at validation */
30+
/** The error message to display if the field is required but not present at validation */
3131
requiredFieldMessage: PropTypes.string,
32-
/* An optional validation function. Will be passed in order: the current field value, and the entire form value */
32+
/** Should an error message render on load (the rendered msg depends on the presence of required or validator) */
33+
validateOnLoad: PropTypes.bool,
34+
/** An optional validation function. Will be passed in order: the current field value, and the entire form value */
3335
validator: PropTypes.func,
34-
/* An optional nested path at which to access field data. Nested path can be delimted with `.` or with brackets `[]`. For example: my.nested.array[0] */
36+
/** An optional nested path at which to access field data. Nested path can be delimted with `.` or with brackets `[]`. For example: my.nested.array[0] */
3537
path: PropTypes.string,
36-
/* Alternate inline display format */
38+
/** Alternate inline display format */
3739
inline: PropTypes.bool,
3840
/** Width of the inline label */
3941
inlineLabelWidth: PropTypes.number,
@@ -53,6 +55,7 @@ const defaultProps = {
5355
description: '',
5456
required: false,
5557
requiredFieldMessage: 'Required field',
58+
validateOnLoad: false,
5659
validator() {},
5760
path: '',
5861
inline: false,
@@ -68,6 +71,7 @@ const defaultProps = {
6871
export const formInputInterface = omit(
6972
[
7073
'requiredFieldMessage',
74+
'validateOnLoad',
7175
'validator',
7276
'className',
7377
'description',

packages/react-components/source/react/library/form/internal/FormFieldElement.js

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const FormFieldElement = props => {
5858
'className',
5959
'style',
6060
'requiredFieldMessage',
61+
'validateOnLoad',
6162
'validator',
6263
],
6364
props,

packages/react-components/source/react/library/form/internal/methods.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ export const updateFieldProps = (
189189
error,
190190
required,
191191
requiredFieldMessage,
192+
validateOnLoad,
192193
validator,
193194
} = userProvidedFieldProps;
194195

@@ -197,7 +198,7 @@ export const updateFieldProps = (
197198

198199
let blockingError;
199200

200-
if (validate) {
201+
if (validate || validateOnLoad) {
201202
if (required && isEmpty(value)) {
202203
blockingError = requiredFieldMessage;
203204
} else if (validator) {
@@ -214,7 +215,7 @@ export const updateFieldProps = (
214215
* Form.Field
215216
*/
216217
const fieldProps = omit(
217-
['requiredFieldMessage', 'validator', 'error', 'path'],
218+
['requiredFieldMessage', 'validateOnLoad', 'validator', 'error', 'path'],
218219
userProvidedFieldProps,
219220
);
220221

0 commit comments

Comments
 (0)