GenericForm is a React form validation plugin. Each form field can use a validation prop to define what format is expected.
$ yarn add generic-form
The library uses a form ID to handle form validation. You may use the GenericForm component with a fields prop to create your form or you can use standard JSX and inject GenericFormField components into the form by specifying the formId prop.
<GenericForm
id="my-form"
fields={ [
{
type: GenericFormFieldType.EMAIL,
id: 'my-form-email',
name: 'email',
validation: {
mandatory: true
}
},
{
type: GenericFormFieldType.PASSWORD,
id: 'my-form-password',
name: 'password',
validation: {
mandatory: true
}
},
{
type: GenericFormFieldType.SUBMIT,
value: 'Login'
}
] }
/>
<GenericForm id="my-form">
<GenericFormField
formId="my-form"
type={GenericFormFieldType.EMAIL}
name="email"
id="my-form-email"
validation={{ mandatory: true}} />
<GenericFormField
formId="my-form"
type={GenericFormFieldType.PASSWORD}
name="password"
id="my-form-password"
validation={{ mandatory: true}} />
<GenericFormField
formId="my-form"
type={GenericFormFieldType.SUBMIT}
value="Login" />
</GenericForm>
marks the field as mandatory, if value isn't set by user it will display the 'errorEmpty' string
The string will be displayed if the field is empty and the mandatory boolean was set
The customErrorHandlers prop allows you to define functions that will check the field's value and return the key you defined as an error if the check is truthy
<GenericFormField
validation={ {
customErrorHandlers: {
'Do not enter "Bad value"': v => v === 'Bad value'
}
} } />
The positiveRegex prop allows you to define props that will trigger errors when the value of the field matches the regex.
<GenericFormField
validation={ {
positiveRegex: {
'No commas are allowed': /,/
}
} } />
The negativeRegex prop allows you to define props that will trigger errors when the value of the field doesn't match the regex.
<GenericFormField
validation={ {
negativeRegex: {
'The field must contain a comma': /,/
}
} } />
The group prop marks a field as part of a group, at least one (or groupMin) fields in a group should be valid for the form to be validated, if the requirement isn't met the field will display the errorGroup prop
<GenericFormField
validation={ {
group: 'field-group',
groupMin: 1,
errorGroup: 'At least one of these fields is required'
} } />
The identicalGroup prop marks a field as part of a group where all values must be equal. If the requirement isn't met the field will display the errorIdenticalGroup prop.
<GenericFormField
validation={ {
identicalGroup: 'field-group',
errorIdenticalGroup: 'These fields must be identical'
} } />
This prop disables a field until all fields in the form are valid:
<GenericFormField
formId="test"
type="submit"
disableUntilValid />
This crops the fields value to a given length (even for non number types)
GenericFormField is always controlled by default if no value or defaultValue is set we use an empty string, however you may override this behaviour with the defaultEmptyValue prop
Add a string to the label (can be usefull for accessibility and/or to show that an input is required)
The package supports external library components through the GenericFormField static method 'registerExtraType'.
A string to identify the new GenericFormField type
An object that has two methods, render and getValue:
The render method should return the custom component.
A method scoped to the component that allows access to component props and should return the component value.
Here is an example of how to register a custom component into the form validation using the react-text-mask plugin.
GenericFormField.registerExtraType('mask', {
render(sharedProps, { mask }) {
return <TextMask
{ ...sharedProps }
mask={ mask }
guide={ false }
type='text'/>;
},
getValue() {
return this.el.current.inputElement.value;
},
});
By registering an extra type you may then use the component in your forms by simply using the string you registered:
<GenericFormField
id='masked-field'
name='masked-field'
type='mask'
mask={[/\d/, /\d/, /\d/, /\d/,'-', /\d/, /\d/]} />
This method allows you to set a global suffix for required labels
GenericFormField.setRequiredLabelSuffix('*');
You can override this value for any input by using the requiredLabelSuffix props
An example is provided with the repository, you may start the parcel dev server by running the following command:
$ yarn example
$ yarn build