angular 5 template forms detect change of form validity status

Multi tool use
Multi tool use


angular 5 template forms detect change of form validity status



are reactive forms the way to go in order to have a component that can listen for changes in the validity status of the form it contains and execute some compoment's methods?



It is easy to disable the submit button in the template using templateRef like [disabled]="#myForm.invalid", but this does not involve the component's logic.


[disabled]="#myForm.invalid"



Looking at template forms' doc I did not find a way




2 Answers
2



If you want to get only the status and not the value you can use statusChanges


status


value


statusChanges


export class Component {
@ViewChild('myForm') myForm;
this.myForm.statusChanges.subscribe(
result => console.log(result)
);
}



If you even want data changes, uou can subscribe to the valueChanges of the form and check the status of the form using this.myForm.status


valueChanges


form


this.myForm.status


export class Component {
@ViewChild('myForm') myForm;
this.myForm.valueChanges.subscribe(
result => console.log(this.myForm.status)
);
}



Possible values of status are, VALID, INVALID, PENDING, or DISABLED.



Here is the reference for the same





any reason for downvote?
– Sravan
Apr 5 at 7:29





Don't know who did the downvote... Since myForm would be an instance of AbstractControl, it is even better to subscribe to form.statusChanges which returns an observable of VALID, INVALID, PENDING or DISABLED. I'm actually a bit worried about PENDING as I don't know its meaning...
– Cec
Apr 5 at 7:34





Pending is when the Angular is in middle of a validation check, when it starts validating and going through all the checks, the status starts pending and it eventually change to either Valid or Invalid
– Sravan
Apr 5 at 7:38


pending


Valid


Invalid





and If you dont need the data changes, you can use statusChanges
– Sravan
Apr 5 at 7:41


statusChanges





So I can just ignore PENDING and wait for a new stats value to be emitted. Thank you very much :)
– Cec
Apr 5 at 7:59



You can subscribe to the whole form changes and implement your logic there.


@ViewChild('myForm') myForm;

this.myForm.valueChanges.subscribe(data => console.log('Form changes', data));






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Eg3WvC,n qGdpItLZ1M52tJAy y bodpeBU,I 2UFe,z,NyVI0KRKpx3ZxaqEt
Ikr,9E 00BFnqpqTP,pwdeS

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications