TL;DR: React provide controlled component, HOC, that is the base idea for React validation library in NPM. However, the weak point is integration with existence Component such as React Select and custom display place of error message
I am migrating from traditional coding jQuery and PHP (used for real and big project for years, not student learn to play code). I focus most of my time on FORM because client's request always about FORM.
Now with FORM, the most important part is validation method/plugin. First forget about some guys tell us about "With React you could easily do controlled component so we do not need validation plugin". Our FORM in production required hundred of Field with many Tabs (for example a personnel form or some crazy organization report), so coding per field is not practical, it s like traditional js coding to validate form.
Come to library, I test and found these three maybe good enough.
I will not go into detail about these lib, but the way they work is similar. We must create a component for input, select, textarea and form to make them work. Inside Input component, we define how Input border change class to make border error red and How error message apprear (in div or span below Input).
(There are other validation libs but their method is a json validation for form or even create form with json, that s not my choice because I want to validate form just by a simple attribute in input such as [required, email]
, not wasting time on ton of definition json)
Now I stuck with this technique in these case:
1. Integration with existence excellent component
I download excelent component from NPM to solve some specific function (like React Select). Now to to the real job, we must validate input for these custom component. This come to an extra work, we must integrate validation to any extra component found. I bang my head to wall to figure out using HOC to validate React Select like this (React-Validation code). And to make this work, I must modify origin HOC to create a custom HOC.
// Define own Input component
function MySelect({ error, isChanged, isUsed, ...selectProps }) {
return(
<div>
<Select onChange={selectProps.onChange.bind(this)} {...selectProps} {...( isChanged && isUsed && error ? {
className: `is-invalid-input ${selectProps.className}`
} : { className: selectProps.className } )} />
<input type="hidden" {...selectProps} />
{isChanged && isUsed && error}
</div>
)
}
const MyValidationSelect = controlSelect(MySelect); //My custom HOC
Now, I cannot think more to work with this technique in future. Imagine we have project and need an extra component. We do not have much time to code our self so download a library. BOOM*, we bang our head to wall to figure out how to make custom component validate. We will waste time on "side project", not the main task (client request feature).
2. Custom place for validation message
Component is good, but it also wrap code inside a layout. Error message must be a part in Input Component. Now come the hard part, some crazy client require to put error message on top of form, or in a modal box. In this case, I still cannot think out a solution if using wrapping Input and error in component.
3. My dirty solution
jQuery appear for long enough for jQuery lib become mature. Validation with jquery could be solve with so called jQuery Validation . Any case we think out for valition could be solved easily and elegant with this lib (custom error placement, custom field, custom validator, custom error format (not just css thing)...)
I just to make jQuery validation with with React form in ComponentDidMount
and it work as it is. It is also simple to integrate validation for any custom react component by writing to a "validation config file" with errorPlacement
, highlight
, success
function API.
Thanks for anyone reach this line, this is really a long topic. And here is my question: I tried to "think in react" but cannot solve the simplest thing: React form validation.
Thanks for any hint for me to solve this issue.