I think i have a very common question about form handling in angular, but don't find any answer for my question after searching and reading for hours.
The problem:
I built a reactive form and if the user is in the angular context (typing values etc.), it's all fine. But when i use a chrome plugin like Web Developer Form Filler
and fill the form with it, the model doesn't update.
To reproduce the problem, i've created a tiny app here: https://stackblitz.com/edit/angular-ltrmpd.
Form controller:
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
form = new FormGroup({
name: new FormControl('Valid name', [
Validators.required,
Validators.minLength(5)
])
});
get name() { return this.form.get('name'); }
onSubmit() {
// reload all form data and revalidate, but how?
if (this.form.valid) {
alert('VALID');
}
}
}
Form view:
<form [formGroup]="form" (ngSubmit)="onSubmit()">
<div class="form-group row">
<label for="name" class="col-sm-2 col-form-label">Name</label>
<div class="col-sm-10">
<input formControlName="name" type="text"class="form-control" id="name" [class.is-invalid]="name.invalid && name.touched" />
<pre class="invalid-feedback">{{ name.errors | json }}</pre>
</div>
</div>
<pre>{{ form.value | json }}</pre>
<pre>form.valid: {{ form.valid }}</pre>
<button type="button" class="btn btn-danger" onclick="document.getElementById('name').value = 'X'">SET VALUE FROM OUTSIDE</button> model doesn't update <br /><br />
<button type="button" class="btn btn-success" (click)="name.patchValue('YYY')">SET VALUE ANGULAR WAY</button> updates the model right <br /><br />
<button type="submit" class="btn btn-primary" [disabled]="!form.valid">Submit</button>
</form>
- The default form value is valid
- Click button "SET VALUE FROM OUTSIDE" -> the model doesn't update
- Click "Submit" -> You should not be able to click the button or we update the model and validation state manually after clicking the button
The simplest solution i tried is to update the model and revalidate after the user submits the form. But how can i update the model with the current form values?
I only found this question: How to update Angular 2 Reactive form field when changes occurred outside of Angular world. But i think it's not the same problem, because i don't have any control over this form filler tools.
inputevent afterward? Does it resolve your problem? Anyway, you must notify Angular that values have been changed if they were changed from outside world. stackblitz.com/edit/angular-jybsvy?file=src/app/… - wannadream