I am trying to use React Hook Form to setup a custom input field and thus use the useForm hook as below;
const {
control,
handleSubmit,
register,
formState,
setValue,
getValues
} = useForm({
mode: 'onSubmit',
reValidateMode: 'onSubmit',
shouldFocusError: true,
defaultValues: {
myIdFld: '123'
},
resolver: yupResolver(schema)
});
const onSubmit = (data) => {
alert(JSON.stringify(data));
};
const onErrors = (data) => {
alert('Errors !');
};
I have a field config js as below;
let sampleField = {"id":"myIdFld","mandatory":true,"visible":true,"maxLength":5,"items":[],"gridItems":[],"disabled":false,"hidden":false,"readonly":false,"fieldLabel":"My ID 1","pattern":/[A-Za-z]{3}/,"editable":true};
I use the above and pass it to my custom Field component JSX as below;
return (
<>
<form
onSubmit={handleSubmit((data) => console.log('Save Successful ', data))}>
<Field name={sampleField.id} control={control} label={sampleField.fieldLabel} formFieldProps={{ necessity: (sampleField.mandatory === true) ?'required': 'optional' }} field={sampleField} {...props} />
<Button onClick={handleSubmit(onSubmit, onErrors)} type="submit" variant="cta">Submit</Button>
</form>
</>
)
My Field component JSX is as below;
import { Controller } from "react-hook-form"
<Controller
control={control}
rules={{required: "The field is required"}}
name={name}
render={({ field: { ref, ...restField }, fieldState: { error } }) => {
const validationMessage = error ? error.message : helperText;
const validationStatus = error && 'error';
return (
<FormField validationStatus={validationStatus} {...formFieldProps}>
<FormFieldLabel {...formFieldLabelProps}>
{label}
</FormFieldLabel>
<Input
inputRef={ref}
{...restField}
{...restProps}
/>
<FormFieldHelperText>{validationMessage}</FormFieldHelperText>
</FormField>
);
}}
/>
My Input is kind of a custom controlled component and I am trying to integrate via Controller
Now, the issue is while on submit, I do get the value which I enter in the textbox alerted (which means the field is getting registered). But I am not able to add any validations and cannot get the error object populated.
Please suggest what I need to add in the above code so that the validations/errors can be integrated. Ideally, I am wanting to use the sampleField config js for validation purpose (since it would have attributes like mandatory: true, regex/pattern, etc ...though, I am not sure if I also need to provide the custom error messages.
If yes, I am not sure where can I add those error configurations.
I already tried adding below and passing rules={createValidationRules(field)} to Field;
const createValidationRules = (fieldConfig) => {
const rules = {};
if (fieldConfig.mandatory) {
rules.required = fieldConfig.customRequiredMessage || "This field is required";
}
if (fieldConfig.pattern) {
rules.pattern = {
value: fieldConfig.pattern,
message: fieldConfig.customPatternMessage || "Invalid format"
};
}
if (fieldConfig.maxLength) {
rules.maxLength = {
value: fieldConfig.maxLength,
message: fieldConfig.customMaxLengthMessage || `Maximum length is ${fieldConfig.maxLength}`
};
}
return rules;
};
But still does not work.