TypeScript Decorator Based Validation
New Way to Validate the Form in VUE
Pure Model-Driven Reactive Form Validation
Here we will discuss an elegant way to validate the form based upon the designed model.
After getting a good response from the Angular Community on decorator based reactive form validation, So I have decided to provide the same feature of Model-Driven Reactive Form Validation for VUE Community :smile: .
Introducing @rxweb/reactive-forms
If I am talking about typescript, the typescript programming language highly relies on Object-Oriented Programming practices, which makes more power to write clean code. This is the main reason to design this validation framework to provide a model object based reactive form. Because this gives you more flexibility when the complexity of application form increase and application grows at that time you don’t bog down the cumbersome activity of assembling the validators and write the same business logic multiple time.
Here is the high-level code of the model:
In the code example, we have defined the property of userName in the class of User and applied the three validation decorator which are alpha , minLength , required . Those can be automatically applied when we create the reactive form group object in the component.
Really :open_mouth:!.
Yes, You don’t need to write additional code for validation in the component or the template:grinning:.
Let’s begin the implementation in the sample project of VUE.
First, We have to install the package in the project.
npm install @rxweb/reactive-forms
After installing the package we have to configure the validation messages and set the value of client library property in the main file of the project. I am writing the below code in the main.ts file.
Let’s use the User Model in the component and create the FormGroup Object, which we will use in the template.
Here is the code of the component.
As you see the above the code where I have used IFormGroup and RxFormBuilder .
What is the use of IFormGroup?
The IFromGroup is a generic interface that provides access of the methods available in the FormGroup and that tightly binds with your model properties.
What is the use of RxFormBuilder?
This provides a FormGroup object based upon the provided model. Through this, the Form Validation rules are bind on the respective properties.
As you can see we have created the FormGroup in the constructor of the SignUp component. Now, Let’s move forward to the template part.
On the template side, You just use the v-model directive and reference the respective property of your model. Whatever properties are defined in the model those are available in the props object property of FormGroup object.
All Done :sweat_smile:.
Wait!
How to show the Error Message?
For showing the error message, you have to use the errorMessage property of respective FormControl like in our case we want to show the error message of userName property. Here is the code of showing the error message:
You don’t have to put multiple v-if conditions in the template for showing the different validation messages. All things are already managed in the errorMessage property. Here is the output:
What’s the most powerful feature in the package?
As all features are superb and powerful :smiley:, The most which I liked a lot is the Form Directly interact with Model Object instance properties. This gives better flexibility of our code because our business logic will be separated into multiple classes instead of components.
Let me share one simple case like we have created the FormGroup and the FormControl is userName , Now there is a requirement where we want to enhance our Form with First Name and Last Name fields. Bottom of the Form we have to combine the First Name and Last Name field value and show as a Full Name.
Achieving the above case we have to define three more properties in the User model. Here is the code:
As you have seen we have used the getter/setter method for firstName and lastName property because whenever the user enters the value in the respective control textbox then setter method will be called and set the fullName . Here is the output:
What’s the power(:muscle:) of this package?
This provides a lot of features with ease of code:
- Clean Code through validation decorators.
- Pure Model-Driven.
- Support Class Abstraction.
- Strongly-Typed.
- Conditional Field Validation.
- Dirty Check.
- Dynamic Validation.
- 45+ Validation Decorators.
- Extensive properties of FormGroup and FromControl like ‘valid’, ‘invalid’, ‘disabled’, ‘errorMessage’, ‘errorMessages’, etc.
- Runtime Validation Rule Change Conditionally.
- Globally configure error message binding strategy.
- Support Multilingual Error Messages.
- Many More….
Here is the working example.
I hope you like this article. If you have any suggestions on this approach, please write your comment below.
For more information about dynamic forms, please refer to this link: rxweb.io
以上所述就是小编给大家介绍的《New Way To Validate the Form in VUE》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Kotlin程序员面试算法宝典
孙伟、楚秦 / 机械工业出版社 / 2018-12 / 69
本书是一本讲解程序员面试笔试算法的书籍。在写法上,除了讲解如何解答算法问题以外,还引入了例子辅以说明,以便读者能够更加容易地理解。 本书将程序员面试笔试过程中的各类算法类真题一网打尽。在题目的广度上,通过各种渠道,搜集了近3年来几乎所有IT企业面试笔试算法高频题目,所选择题目均为企业招聘使用题目;在题目的深度上,本书由浅入深、庖丁解牛式地分析每一个题目,并提炼归纳,同时,引入例子与源代码、时......一起来看看 《Kotlin程序员面试算法宝典》 这本书的介绍吧!
MD5 加密
MD5 加密工具
HEX HSV 转换工具
HEX HSV 互换工具