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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
点击的奥秘:运用说服心理术提升在线影响力(全彩)
Nathalie Nahai(娜塔莉.纳海) / 陈旭 / 电子工业出版社 / 2014-9-1 / 75.00元
用户的每一次点击,不管是在虚拟商店购物,还是在浏览企业网站,或是漫无目的地把玩手机,都蕴藏着基于心理学的无穷奥秘。《点击的奥秘:运用说服心理术提升在线影响力》作者为全球知名的网络心理学家,其在《点击的奥秘:运用说服心理术提升在线影响力》中将心理学、神经科学及行为经济学巧妙地结合在一起,挖掘和提炼出一套行之有效的网络用户引导策略——既涵盖在线说服最新研究动向,也包括最前沿的科技成果,以及其他诸多惊人......一起来看看 《点击的奥秘:运用说服心理术提升在线影响力(全彩)》 这本书的介绍吧!