I have a form in which there are 4-5 different types of control. On certain user action, I need to know if any of the controls have any value in it and it can happen on any state of the form - be it pristine or dirty. I cannot rely on the form states for this. cCan't even loop through since this.myForm.controls isn't an array type. Also this.myForm.value is always an 'object' even though no values for controls in it.
Here is the form creation code, if that helps:
this.searchForm = this.fb.group({
'ids': this.fb.control([], multipleInputValidator(2)),
'locationName': this.fb.control([], Validators.minLength(2)),
'accountCodes': this.fb.control([], multipleInputValidator(2)),
'regionCodes': this.fb.control([], multipleInputValidator(2)),
'city': this.fb.control([], Validators.minLength(2)),
'typeIds': this.fb.control([]),
'centreIds': this.fb.control([]),
'siteCodes': this.fb.control([]),
'statusCode': this.fb.control([]),
'from': this.fb.control([]),
'to': this.fb.control([])
});
You can use rxjs operators to check values.
console.log(!!this.myForm.get('mycontrol').value);
Here's a quick way you can do this check using Object.keys().
Object.keys(this.searchForm.value).some(k => !!this.filterForm.value[k])
This will check the properties in the value
object that represents your form state, and return true
if any of those properties are truthy, i.e. have a value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With