Тёмный
No video :(

Move validation messages to the component class in reactive form 

kudvenkat
Подписаться 832 тыс.
Просмотров 53 тыс.
50% 1

In this video we will discuss, how to move validation messages to the component class.
Healthy diet is very important for both body and mind. We want to inspire you to cook and eat healthy. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking.
/ @aarvikitchen5572
Text version of the video
csharp-video-tu...
Slides
csharp-video-tu...
Angular 6 Tutorial
• Angular 6 tutorial for...
Angular 6 Tutorial Text Articles & Slides
csharp-video-tu...
Angular, JavaScript, jQuery, Dot Net & SQL Playlists
www.youtube.co...
There are several benefits to this
1. Easily unit test validation logic
2. Instead of hard-coding validation messages in the application, we can load them from an external source like a database or a file.
3. Change validation dynamically at run-time based on the decisions made in code or user selections
In this video let's move the validation messages to the component class and in our next video we will discuss moving the logic to show and hide these messages.

Опубликовано:

 

27 авг 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 46   
@belmiris1371
@belmiris1371 5 лет назад
These videos are gold. It's generous of you to share them, thank you!
@ShahnoorShaikh
@ShahnoorShaikh 5 лет назад
Thank you for sharing this Video.
@peterl1699
@peterl1699 5 лет назад
Great video. Will you go over multiple language support?
@anantbalwantbodas4971
@anantbalwantbodas4971 5 лет назад
It would help understand code in logValidationErrors method, if explained using debugger.
@aakashchauhan96
@aakashchauhan96 2 года назад
Getting below error please help me out- Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ fullName: { required: string; minlength: string; maxlength: string; }; email: { required: string; }; skillName: { required: string; }; experienceInYears: { required: string; }; proficiency: { required: string; }; }'. No index signature with a parameter of type 'string' was found on type '{ fullName: { required: string; minlength: string; maxlength: string; }; email: { required: string; }; skillName: { required: string; }; experienceInYears: { required: string; }; proficiency: { required: string; }; }'
@ranjithreddy7985
@ranjithreddy7985 2 года назад
Update this and try validationMessages : any = { 'fullName': {
@Fetretim
@Fetretim Год назад
@@ranjithreddy7985 That cost me 2 days.. Thank you sir..
@ashishkapoor7851
@ashishkapoor7851 3 года назад
I am not able to extract validatiomessage property value by putting key as parameter in square bracket it is showing index signature error plz help with it i am stuck here from 3 days
@ashishkapoor7851
@ashishkapoor7851 3 года назад
Line no 61 of create employee component .ts Only i can right This.validationMessages; If i right this.validationMeaasge[key]; It is throwing error index signature ts(7053).
@jobiraw
@jobiraw 3 года назад
do you get the solution for this??
@jobiraw
@jobiraw 3 года назад
to solve this issue use "// @ts-ignore " comment above the line
@gauravg0988
@gauravg0988 5 лет назад
I am also facing the same issue. Hence I tried to print this.validationMessages['fullName'] and the error was displayed on console. I could infer from this that validationMessage array is not initialized
@vijayu707
@vijayu707 5 лет назад
Hi, Thank you for the video. I am not able to understand the purpose of moving the validation and error messages from template to the component class. What is the advantage? Where it is helpful? please do explain. Thanks
@Csharp-video-tutorialsBlogspot
Hi Vijay - There are several benefits of moving validation and error messages to the component class. They are as follows 1. Easily unit test validation logic 2. Instead of hard-coding validation messages in the application, we can load them from an external source like a database or a file. 3. Change validation dynamically at run-time based on the decisions made in code or user selections.
@juturisharon3075
@juturisharon3075 5 лет назад
Awesome
@sunpreetsingh5143
@sunpreetsingh5143 5 лет назад
for email you can also add in email validation mgs as "email" : "not a valid email id",
@osamahashim
@osamahashim 5 лет назад
Great work, many thanks, But could you make numbers for lesson litle while uploading please?
@Csharp-video-tutorialsBlogspot
Hello Osama - The Angular 6 playlist, at the following link, has all the videos in logical sequence. Hope this helps. ru-vid.com/group/PL6n9fhu94yhWNJaDgh0mfae_9xoQ4E_Zj You can find all our course playlists at the following link. ru-vid.complaylists?view=1&sort=dd
@sashtana5419
@sashtana5419 5 лет назад
Hi Venkat Sir, i have been following your videos for long. I am facing an error 'Cannot read the property 'fullName' of undefined. I am getting this error on the following line of the code. before that i didnt face any problem. for (const errorKey in abstractControl.errors){ if(errorKey) { this.formErrors[key] += messages[errorKey] + ' '; // on this specific line is the error. } } Please help. It gives error when it enters the loop for the first time thats why it says fullName is undefined.
@saicharan9786
@saicharan9786 3 года назад
i got same error, and got resolved i have included this formError : { fullName: '', email: '', skillName: '', experienceInYears: '', proficiency: '', }; Instead of formError = { fullName: '', email: '', skillName: '', experienceInYears: '', proficiency: '', };
@ashifali2817
@ashifali2817 5 лет назад
you can remove + and use like this.formErrors[key] = messages[errorKey] + ' '; in place of this.formErrors[key] += messages[errorKey]; But ur vdo is awsome.Thanks dear
@vinothkumar-mc3si
@vinothkumar-mc3si 5 лет назад
Bro what is the difference between two statements like you mentioned above statements
@susheelyadav6143
@susheelyadav6143 5 лет назад
bro that forEach loop will give you one by one errors on each iteration. so you have to add these errors. that's why he used it "this.formErrors[key] += messages[errorKey];". and in your case only last error will be shawn bc all errors wil override.
@vigneshgovindharaj5234
@vigneshgovindharaj5234 5 лет назад
Sir I am new to coding i am getting Cannot read property 'required' of undefined on the console. please help me
@bhavyamanasak4395
@bhavyamanasak4395 4 года назад
it means you need to declare/Define it first.
@jangamvibhuthi8488
@jangamvibhuthi8488 5 лет назад
ERROR TypeError: Cannot read property 'fullName' of undefined : Sir i am getting the error .. i have written same as your code.please suggest me
@Csharp-video-tutorialsBlogspot
Hello Vibhuthi - Can you please check which line is causing this error. To figure this out find all references to fullName property, comment all the references except one and see if you get the error. Use this technique to isolate the problem to the line that actually causing this error. Hope this helps to nail down the issue.
@RaghavendraGowda7
@RaghavendraGowda7 5 лет назад
Make Sure ValidationMessage Json is valid!
@gauravg0988
@gauravg0988 5 лет назад
I am also facing the same issue. Hence I tried to print this.validationMessages['fullName'] and the error was displayed on console. I could infer from this that validationMessage array is not initialized
@saicharan9786
@saicharan9786 3 года назад
formError = { fullName: '', email: '', skillName: '', experienceInYears: '', proficiency: '', };
@surenrajmuthuraj1526
@surenrajmuthuraj1526 5 лет назад
I can't run Ng serve -o command
@sandeepchegu6849
@sandeepchegu6849 5 лет назад
ng serve --open Or ng serve -o
@Csharp-video-tutorialsBlogspot
Thank you Sandeep. You can also make it even shorter ng s -o
@sandeepchegu6849
@sandeepchegu6849 5 лет назад
@@Csharp-video-tutorialsBlogspot yes sir I am using the same as u said
@ymtan
@ymtan 5 лет назад
Hi Venkat, I don't really understand the following code. Why do we need to loop over the abstractControl.errors ??? for (const errorKey in abstractControl.errors) { if (errorKey) { this.formErrors[key] += messages[errorKey] + ' '; } } Best regards, Edward
@Csharp-video-tutorialsBlogspot
messages constant contain all the validation errors of a form control, where as the abstractControl.errors property contain the names of the validation rules the form control has failed. For example, if fullName control has failed required validation, the required key will be in the errors collection of abstractControl. So from the messages constant we need to retrieve the validation message for required error key. If in the future we add a form control that can possibly have multiple validation rules failing, we want validation messages of all those failing error keys, hence the loop. Hope this answers your question.
@ymtan
@ymtan 5 лет назад
I still cannot understand how the following codes work. If the errorKey constant is ''required'' validation rule, then messages[errorKey] contain 'Full Name is required.' validation message and this message will be stored in the fullName property of the formErrors object. Am I correct ??? if (abstractControl && !abstractControl.valid) { const messages = this.validationMessages[key]; for (const errorKey in abstractControl.errors) { if (errorKey) { this.formErrors[key] += messages[errorKey] + ' '; } } }
@Csharp-video-tutorialsBlogspot
Yes. That is correct.
@ymtan
@ymtan 5 лет назад
I am still struggling to understand the following block of code. const messages = this.validationMessages[key]; for (const errorKey in abstractControl.errors) { if (errorKey) { this.formErrors[key] += messages[errorKey] + ' '; } } If the fullName form control has failed required validation, minimum length validation and maximum length validation, could you kindly explain how the above block of code works in order to retrieve all the 3 associated validation messages from the validationMessages structure and store those validation messages in the fullName property of the formErrors object ???
@Csharp-video-tutorialsBlogspot
First of all, the following 3 validators are mutually exclusive, meaning at any given point only one of them can fail. required minimum length maximum length However, for the example sake, let's assume, both required and minLength validators have failed. In this case 1. abstractControl.errors property will have both the keys - required and minlength (AbstractControl in this context is refferring to fullName form control) 2. messages constant will contain all the 3 validation messages (for required, minLength and maxlength) 3. The for loop executes 2 times, because in the abstractControl.errors property we have 2 keys (required and minlength ) 4. The first time when the for loop completes execution it contains validation message for the required error formErrors['fullName]='Full Name is required. ' The second time when the for loop completes execution it contains validation message for the minlength error along with the required error. So formErrors['fullName] looks as shown below, displaying both the messages with a single space between them. formErrors['fullName]='Full Name is required. Full Name must be greater than 2 characters' Hope this clears your confusion
@kevinrobinson6685
@kevinrobinson6685 4 года назад
Not great if you have multiple forms. Your validation code is going to be repeated. Better to move this code to a separate class or better still a custom npm repository. Also never write code in your ngOnInit or constructor methods like this. Write a function and call thr function.
Далее
Liskov: The Liskov Substitution Principle
4:23
Просмотров 21 тыс.
Only I get to bully my sister 😤
00:27
Просмотров 28 млн
I Took a LUNCHBAR OFF A Poster 🤯 #shorts
00:17
Просмотров 4,8 млн
Angular reactive forms validation
13:54
Просмотров 125 тыс.
Angular reactive forms
10:49
Просмотров 203 тыс.
Google Data Center 360° Tour
8:29
Просмотров 5 млн
The Growing Semiconductor Design Problem
16:41
Просмотров 276 тыс.
What I WISH I Knew Before Playing The War Within
12:15
Brutally honest advice for new .NET Web Developers
7:19
Angular reactive forms cross field validation
15:56
Просмотров 36 тыс.