Reactive Forms in Angular - Dynamic Validation

  Рет қаралды 9,270

Decoded Frontend

Decoded Frontend

Күн бұрын

Пікірлер: 50
@DecodedFrontend
@DecodedFrontend 3 ай бұрын
Learn Everything About Angular Forms 🚀 bit.ly/advanced-angular-forms_yt
@rembautimes8808
@rembautimes8808 3 ай бұрын
The angular forms course is gold quality. I subscribed last year and helped me a lot in my development
@DecodedFrontend
@DecodedFrontend 3 ай бұрын
I am so happy to hear that! Thank you for sharing your experience! P.s. I swear the comment wasn't a paid ad 😅
@artempushnev1855
@artempushnev1855 2 ай бұрын
😂​@@DecodedFrontend
@ferhado
@ferhado 3 ай бұрын
I have subscribed to the Angular Forms course. No words, it's just amazing. I highly recommend it to everyone who wants to reach an advanced level in Angular Forms.
@ferhado
@ferhado 3 ай бұрын
Dmytro, could you please make a video about Angular Material theming for v18? The documentation is confusing.
@nedafayazi
@nedafayazi 2 ай бұрын
I wish I could like your Angular Form course million times. Thank you very much for your generusity in teaching complex issues in a very simple way.
@Nabulio85
@Nabulio85 3 ай бұрын
Thank you Dmytro. Great content. I have a question about your course. Are you updating its content with the features of the latest versions of Angular? (17 and 18 for example)
@MateusBellomo-ml4zm
@MateusBellomo-ml4zm 3 ай бұрын
I was time travelling here, nice video.
@DecodedFrontend
@DecodedFrontend 3 ай бұрын
Haha, yes :)
@DrCuttler
@DrCuttler 3 ай бұрын
Very cool! I actually did the same thing recently, but did it slightly differently: a) I added the Validator to the field permanently but enabled and disabled them instead of added and remove them b) I transferred the valueChanges observable into a signal and used angular effects for switching. BTW: I really enjoy your channel! You made me a better angular developer! :)
@RahafHaj
@RahafHaj 3 ай бұрын
I think using cross validation is better when control validation is related to other control values within the form, where we can get the year value from form.value.year and check it conditionally every time conditionalPassportRequired(control: AbstractControl) { const {year, passport} = form.value; if(year > x && !passport) { return {passportRequired: true} } return null }
@DecodedFrontend
@DecodedFrontend 3 ай бұрын
Hi, Thanks for your suggestion. As always, in web development, things might be done in many different ways, and each solution has its own pros and cons. Let’s see if the cross-validation at the root FormGroup is indeed a better solution: The cross-validator on the root level of the form will invalidate the root FormGroup and not a particular control (passport). This will lead to multiple problems: 1. Inconsistent error checking. The error object returned by the validator will be added to the root FormGroup.errors. This means that in order to check the passport field against the ‘required’ error, you would need to do it in the root FormGroup like this.form.hasError('passportRequired’). To me, it is quite confusing and not obvious because the error object should “live” in the control it belongs to and not in the root FormGroup. 2. Inconsistent control validity state. From an Angular point of view, your passport control will remain valid. This means that it will get the ng-valid CSS class. In this case, you might encounter a situation where your field got a green border (indicating a valid state), but below will be displayed an error message that the field is required. This is quite confusing UX. 3. Scaling. At first glance, this solution wouldn’t scale well. In an actual application, you usually have an object/map like “validatorKey -> Error Message” to keep validation messages consistent across the whole application. Because you introduce a new error key “passportRequired”, you would need to add this key also to the error message map, and the message for this new key should most probably be the same as the message for the regular ‘required’. So if later you would need to change the error message for the “required” validator, you have to update it also for “passportRequired”. And here, we are talking only about the passwordRequired case, but in reality, you might have other dynamic validators, which will make error message management quite challenging. 4. Performance impact. Most probably, if the validator logic is lightweight and the form is small, you won’t see a real performance impact; however, it is definitely worth mentioning that the validator in the root FormGroup will be triggered every time when any FormControl value changes in the form. It makes no sense to do that because we are interested only in changes from the yearOfbirth control. Those are the first thoughts that came to my mind this late night :)
@RahafHaj
@RahafHaj 3 ай бұрын
That's was great deceleration, thanks 👍
@ZawilecxD
@ZawilecxD 2 ай бұрын
Hey but what if we assign the validator to the passport FormControl and the change validator code to access "control.parent.year" and return "required" error name :) ?
@RahafHaj
@RahafHaj 2 ай бұрын
I think the problem for this is that when we change code control won't trigger the passport validator
@jeffnikelson5824
@jeffnikelson5824 3 ай бұрын
Why not use new features like the new control-flow or the takeUntilDestroyed operator
@DecodedFrontend
@DecodedFrontend 3 ай бұрын
Because at the moment when this video was recording, those features were not released :)
@koempf
@koempf 3 ай бұрын
I subscribed to this course also last year :-) great course absolutely :-) would be cool if you could update it to Angular 17/18 features :-) especially Signals :-)
@DecodedFrontend
@DecodedFrontend 3 ай бұрын
Once the signals will be adopted by Angular Forms, I will definitely to the update :)
@madeOfClay99
@madeOfClay99 3 ай бұрын
Top quality content! Any plans for making a course all about signals with Angular? Thank you for the video Dmytro
@arwaromdhane6732
@arwaromdhane6732 3 ай бұрын
I want to thank you for your gold videos you helped me a lot ❤❤❤❤❤❤❤❤
@DecodedFrontend
@DecodedFrontend 3 ай бұрын
I'm so glad to hear that! Thank you for the comment :)
@thanhtuanle7186
@thanhtuanle7186 3 ай бұрын
hey, thanks for your wonderful forms course. How can i get the source code of this video to reference?
@DemystifyFrontend
@DemystifyFrontend 3 ай бұрын
You are an awesome guy, love your videos ❤️
@ugochukwuumerie6378
@ugochukwuumerie6378 3 ай бұрын
Useful video
@cwnhaernjqw
@cwnhaernjqw 3 ай бұрын
Hi Dmytro nice video. Question: do we also have to unsubscribe onDestroy when subscribing to a form control, or angular takes care of that? Thanks
@DecodedFrontend
@DecodedFrontend 3 ай бұрын
Hi :) Thank you! Honestly, I don't remember that the control models have any mechanism of auto-unsubscription, so I would definitely unsubscribe explicitly. I personally follow the rule - Unsubscribe-Always ;)
@syimykamatov8955
@syimykamatov8955 3 ай бұрын
Helpfull video
@vasiliykrush2150
@vasiliykrush2150 3 ай бұрын
For some reason I had expected something better written than what I saw in the video. Like some util. But any case, thanks
@andreiarpenti2589
@andreiarpenti2589 3 ай бұрын
If user selects 10 times an adult date, won't be the validator added 10 times? Or angluar sees that the object is already there, and won't add it many times?
@DecodedFrontend
@DecodedFrontend 3 ай бұрын
Good question. This is what says the comment in the src code about that: “Adding a validator that already exists will have no effect” - github.com/angular/angular/blob/101edda0184b458d331163b7e04280c3b8b48bfc/packages/forms/src/model/abstract_model.ts#L788
@ivanco_s
@ivanco_s 3 ай бұрын
cool 😎
@valikonen
@valikonen Ай бұрын
Why not to use a custom validator?
@pavlokozachuk555
@pavlokozachuk555 3 ай бұрын
Once the yearOfBirth control is defined with an initial value, it makes sense to add a required validator to the passport control when it's defined, and not by using the startsWith operator. Слава Україні!
@DecodedFrontend
@DecodedFrontend 3 ай бұрын
Героям слава 🇺🇦 Yes, it could be an option as well 👍
@GamerOfBharat
@GamerOfBharat 3 ай бұрын
If we do updateValueAndValidity() isn’t it just call the valueChanges() again and again
@DecodedFrontend
@DecodedFrontend 3 ай бұрын
Thanks for the question. Yes, updateValueAndValidity() will trigger the valueChanges and statusChanges events. Whether it is a desired behavior or not depends on your particular use case. Nevertheless, if you don't want those events to be emitted, you can call it like this: updateValueAndValidity({ emitEvent: false })
@marcwinner567
@marcwinner567 Ай бұрын
Is assigning that Validator reference to a local variable for minLength really the "official" way of doing it? I think it would be more cohesive to have some sort of option for it in the method call add/removeValidators. Slava Ukraini!
@vim2741
@vim2741 2 ай бұрын
upload the course in udemy pls
@rockenOne
@rockenOne 3 ай бұрын
Use toSignal instead
@qdp684
@qdp684 3 ай бұрын
Why not just create a validator for a form?
@tarquin161234
@tarquin161234 3 ай бұрын
I'm confused by the assignment to this.skills$. I can't fully see what is being done here as the rest of the file is not shown, but why are you using tap here rather than map? If I'm not mistaken, tap will not return anything, so this.skills$ will never emit anything. I also can't see where the subscriptions to this.skills$ are.
@DecodedFrontend
@DecodedFrontend 3 ай бұрын
Hi, thanks for the question. In the context of exactly this lesson, this.skills doesn't play any role. However, to answer your question, I use the tap operator because the code inside performs side effects (new FormControls are registered), and the tap operator is exactly the place where side effects have to happen. The map operator should be a pure function that simply transforms data from A -> B, but that was not the case there.
@tarquin161234
@tarquin161234 3 ай бұрын
@@DecodedFrontend It's just weird seeing tap with no other operators in the pipe. I was curious to see it's uses. Good video anyway
@DecodedFrontend
@DecodedFrontend 3 ай бұрын
@@tarquin161234 well, the alternative would be to put logic from tap to the subscribe() callback but from the functional point of view it should be the same and it is mostly a matter of taste. I even saw the thread about this topic on Twitter and opinions were split pretty much equally :)
@tarquin161234
@tarquin161234 3 ай бұрын
@@DecodedFrontend sure. But from my pov it's confusing because you can't see any subscriptions so looks like the tap will never execute
@DecodedFrontend
@DecodedFrontend 3 ай бұрын
Ah, ok. Now I see what confuses you. Actually, the subscription to this.skills$ happens via the async pipe in the template. This will trigger an HTTP call inside the getSkills() method, and once data arrives, it goes to the tap operator where, based on returned data, I create corresponding FormControls and add them to the form (calling buildSkillControls()). Because buildSkillControls() performs side effects, I placed it in the tap operator because, in my case (since I subscribe via async pipe), the tap operator is the only place where I could handle side effects. I hope now it is clearer :) It might look strange because you don't know the context of the course and what was done before :)
Deferrable Views - New Feature in Angular 17
15:31
Decoded Frontend
Рет қаралды 21 М.
TOP 6 Mistakes in RxJS code
18:35
Decoded Frontend
Рет қаралды 18 М.
❌Разве такое возможно? #story
01:00
Кэри Найс
Рет қаралды 6 МЛН
Real Or Cake For $10,000
00:37
MrBeast
Рет қаралды 60 МЛН
Or is Harriet Quinn good? #cosplay#joker #Harriet Quinn
00:20
佐助与鸣人
Рет қаралды 47 МЛН
Reactive Form Validation in Angular: Mastering Best Practices
13:08
Monsterlessons Academy
Рет қаралды 5 М.
Zoneless Angular Applications in V18
14:00
Deborah Kurata
Рет қаралды 16 М.
Input Signals in Angular 17.1 - How To Use & Test
14:34
Decoded Frontend
Рет қаралды 26 М.
Web Workers in Action - Performance Boost for Web Applications (2023)
22:51
Content Projection in Angular - Complete Guide (Beginners/Advanced)
26:18
Here's what I've figured out about Angular signals
8:33
Joshua Morony
Рет қаралды 17 М.
How to Make Forms in Angular REUSABLE (Advanced, 2023)
21:10
Decoded Frontend
Рет қаралды 64 М.
Turns out REST APIs weren't the answer (and that's OK!)
10:38
Dylan Beattie
Рет қаралды 156 М.
Reactive Forms  - The Basics
15:48
Fireship
Рет қаралды 256 М.
❌Разве такое возможно? #story
01:00
Кэри Найс
Рет қаралды 6 МЛН