Sorry if it is something easy, but I'm doing my pinnings in Angular 5 but I have the following question.
I would like to validate a FormGroup, I know there are approximations when creating forms, by template and by data, I am currently using the data approach, I have the following scheme .. I have an object:
registro: Object = {
nombre: null,
apellidoPaterno: null,
apellidoMaterno: null,
rfc: null,
curp: null,
telefono: null,
email: null,
passwords: {
password1: "",
confirmPassword: ""
}
};
I make a statement:
forma: FormGroup;
and in the constructor I declare:
this.forma = new FormGroup(
{
'nombre': new FormControl('', [Validators.required]),
'apellidoPaterno': new FormControl('', [Validators.required]),
'apellidoMaterno': new FormControl('', [Validators.required]),
'rfc': new FormControl('', [Validators.required]),
'curp': new FormControl('', [Validators.required]),
'telefono': new FormControl('', [Validators.required]),
'email': new FormControl('', [Validators.required, Validators.email]),
'passwords': new FormGroup({
'password': new FormControl(''),
'confirmPassword': new FormControl('')
}, [Validators.required]
)
});
and my html:
<div class="col-md-12">
<form [formGroup]="forma" (ngSubmit)="guardar()" novalidate>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Nombre:</label>
<div class="col-sm-10">
<input type="text" class="form-control" placeholder="Ingrese su nombre completo" formControlName="nombre" [ngClass]="{ 'is-invalid': !forma.controls['nombre'].valid && forma.controls['nombre'].touched }">
<div *ngIf=" forma.controls['nombre'].touched && !forma.controls['nombre'].valid" class="invalid-feedback">
Es importante conocer su nombre
</div>
</div>
</div>
...
<div >
<div class="form-group row" formControlName="password">
<label class="col-sm-2 col-form-label">Contraseña:</label>
<div class="col-sm-10" formGroupName="passwords">
<input type="password" class="form-control" placeholder="Ingrese su nombre completo" >
{{forma.get('passwords.password').valid}}
<div *ngIf="forma.controls['passwords'].controls['confirmPassword'].invalid">
Invalido
</div>
</div>
</div>
<div class="form-group row">
<label class="col-sm-2 col-form-label">Repita su contraseña:</label>
<div class="col-sm-10" formGroupName="passwords">
<input type="password" class="form-control" placeholder="Ingrese su nombre completo">
{{forma.get('passwords.confirmPassword').valid}}
<div *ngIf="forma.controls['passwords'].controls['password'].invalid">
Invalido
</div>
</div>
</div>
</div>
<button type="submit" class="btn btn-outline-primary btn-block">Guardar</button>
The fact is that the control is validated when the focus is lost. If you apply this sequence of actions it does not work for me:
1) you enter password. It will give you an error in password when losing focus because it is different than confirmPassword (which is empty)
2) you enter as confirmPassword the same one you entered as password. When losing focus confirmPassword will be valid but password is still invalid because its validator is not running.
I would like to validate in real time that the 2 inputs are valid and show the error in the input confirmPassword