New validation package for React on Mobx @ quantumart / mobx-form-validation-kit

Good afternoon.

Today I want to talk about a new package for asynchronous validations in projects that are based on React , Mobx and written in Typescript .

Modern Frontend development involves a large amount of logic when filling out pages with documents, questionnaires and documents for a loan, payment orders, registration pages on the site. The main logical burden falls on validation checks. Angular developers thought over this point and offer developers for these purposes to use the built-in FormControl mechanism, which, although it has a number of drawbacks, is still better than the complete absence of such a solution on React . The situation is complicated by the fact that the current trend of React development involves the use of mobx to organize business logic.

Faced with these problems, we solved all of them by writing a package: @ quantumart / mobx-form-validation-kit



Package advantages:



Instructions for working with the package under the cut.





At the beginning we will describe the functionality of the @ quantumart / mobx-form-validation-kit package, at the end of the article we will write a fully working page with an example of the registration form on the site.



Formcontrol



@ quantumart / mobx-form-validation-kit allows you to create a layer between the source data and the form to display. Which, in turn, allows you to validate them and, if necessary, change the data before they get to the original object.



The @ quantumart / mobx-form-validation-kit library contains three main classes (validation components) for managing the form:



In addition, there are basic abstract classes



Best practice for creating a validating form would be the following idea.

An object of type one FormGroup is created on the form and the fields are already listed in it

this.form = new FormGroup<IUserInfo>({ name: new FormControl( this.userInfo.name, [], v => (this.userInfo.name = v) ), surname: new FormControl( this.userInfo.surname, [], v => (this.userInfo.surname = v) ) // … });
      
      





FormGroup supports nesting, i.e.

 this.form = new FormGroup<IUserInfo>({ name: new FormControl( this.userInfo.name, [], v => (this.userInfo.name = v) ), surname: new FormControl( this.userInfo.surname, [], v => (this.userInfo.surname = v) ) passport: new FormGroup<IPassport >({ number: new FormControl( this.userInfo.passport.number, [], v => (this.userInfo.passport.number = v) ), // … }) // … });
      
      





You can add a FormArray , which in turn can be passed the type FormControl and or the whole FormGroup creating objects of any complexity and structure.




All Articles