on the “next” button i want to verify the form. this works fine, and via “this.personForm.valid” i can verify the form. if the user touchs into the username field, and presses the next button, the field is also marked as red (from $text-input-ios-show-invalid-highlight) and invalid -> perfect.
But if the user does not touch the field and presses the next button, the form is invalid and the input is NOT marked red. When i manually remove the “ng-untouched” css-class from this input element, it is marked as red.
I tried to set the Control as “touched” in various forms:
for (var i in this.personForm.controls) {
this.personForm.controls[i].markAsTouched();
this.personForm.controls[i].updateValueAndValidity({ onlySelf: false, emitEvent: true});
}
this.personForm.markAsTouched();
this.personForm.updateValueAndValidity({ onlySelf: false, emitEvent: true});
with and without parameters for updateValueAndValidity etc.
The control has the right status after this (touched: true), but ionic will not automatticly remove the “ng-untouched” class from the control, even if the control is successfully marked as touched.
Okai, i found out that calling the “markAsTouched()” method on the control actually does remove the ng-untouched and add the ng-touched class on the input, but not on the ion-item. is this a bug or a feature?
something like this will get it work…
note, for some reasons ionic is not highlighting select-input items which are invalid with a red border, only text-inputs. again, is this a bug or a feature?
For example on your template if you are using Reactive Forms you dont need the ngModel directive.
Another thing, here is the way you should create a form:
this.itemForm = new FormGroup({
'name': new FormControl('', [Validators.required,
Validators.maxLength(20),
Validators.pattern('[A-Z][a-zA-Z]*')]),
'quantity': new FormControl('', [Validators.required, QuantityValidator.isValid])
});
thanks for your response!
it doesnt matter if i create the formgroup with the FormControl constructor or, like in my example, with an array for the parameters.
The ngModel is needed in my case because i bind the input directly to my model which i use later, but thats not relevant for this case.
I know that i can additionally show some error textes like in your example, and this will work fine for sure, because you are checking the touched property of the control, not of the ion-item. The red border from ionic will only show up, if the ion-item has the ng-touched class (check the css selector of this).
How do you trigger the validation in your case, if the user does not touch the input but the “next” button or similar?
EDIT: this is the relevant css for the invalid-border on an ion-item:
Hi, did you find the solution? I have the same issue. In my scenario, the first time I touch and then touch outside, the ‘ng-untouched’ still there. and the second time touch, it change to ‘ng-touched’.
Recorro todos los controles y los que no sean validos, les asigno un valor vacio, y los marco como dirty.
for (var controlName in this.fGeneral.controls) {
let ele = document.querySelector(’[formControlName="’ + controlName + ‘"]’);
if(this.fGeneral.controls[controlName].valid) {
export class YourPage {
constructor(
public elementRef: ElementRef
) {
}
submit() {
//remove the 'ng-untouched' class and add the 'ng-touched' attribute
(this.elementRef.nativeElement.querySelectorAll('ion-item') as HTMLElement[]).forEach((x) => {
x.classList.add('ng-touched');
x.classList.remove('ng-untouched');
});
}
}