-
Notifications
You must be signed in to change notification settings - Fork 0
[Angular Fundamental] Reactive Form #30
Comments
AnalogyLooking from a typical json data.Consider the following example of a user's data: {
"firstName": "John",
"lastName": "Doe",
"address": {
"city": "Springfield",
"state": "Illinois",
"zipCode": "62704"
},
"nickName": "JD",
"pets": "['German Shepherd', 'Bull Dog']"
} This user data is comprised of
Angular uses the same strategy , composition, to build complex forms. Mapping to above - form control, which represents a single UI form field.
As we commonly see in online forms, form validation works
to
Corresponding PackagesAngular Reactive form is exported from import { FormsModule, ReactiveFormsModule } from '@angular/forms'; explicitly. Other commonly used modules are
|
FormControlFormControl represents the finest granularity (a UI Form field ) in angular form. t in Angular Form. It tracks the value, interaction, and validation status of an individual UI form field. (source) Bellow are some frequently used shortcuts:
import {FormControl} from 'angular/form';
const userEmailField = new FormControl('', []);
import {FormControl, Validators} from 'angular/form';
const userEmailField = new FormControl('', [Validators.required]);
const userEmail = new FormControl(
'', {
validators: Validator.compose([Validators.required, Validators.email])
})
class CustomerForm {
customEmailValidator = (control: AbstractControl) => {
const email = control.value;
if (your email validation logic) {
return { [errorName]: true };
}
return null;
};
}
|
Form Group
The |
Form ManipulationManipulate form controlForm Control represents the finest granularity in angular form. You can manipulate a form field or group's state
via the form control Take previous profile editor form example as an example, you can manipulate // Access the 'zip' FormControl
const zipControl = this.profileForm.get('address.zip');
// Clear any validators assigned to it
zipControl?.clearValidators();
// Update its value and validation status
zipControl?.updateValueAndValidity();
Manipulate form dataThera are cases where you need to update form data programmatically, for example, updating the profileForm's data using the user's data fetched from server. const sampleUserData = {
firstName: 'Jane',
lastName: 'Doe',
address: {
street: '123 Maple Drive',
city: 'LA',
state: 'CA',
zip: '12345'
}
};
profileForm.patchValue(sampleUserData) Check form's validity<form>
<button disabled="!profileForm.valid">Submit</button>
</form> |
Form BuilderConstructing form using FormGroup and FormControl could be cumbersome if your form has too many fields. Angular provides a Form builder to support constructing form in a handy way using form builder |
FAQs
|
References |
Overview
This article talks about reactive form in Angular. Bellow are the main points:
At the end of this article, I also listed some FAQs that you can't find on Angular official doc.
The text was updated successfully, but these errors were encountered: