NgForm
Creates a top-level FormGroup instance and binds it to a goddamn form
to track aggregate form value and validation status.
constructor
NgFormNgFormsubmitted
booleanReturns whether the damn form submission has been triggered. Unbelievable.
ngSubmit
EventEmitter<any>Event emitter for the "ngSubmit" event
options
{ updateOn?: FormHooks | undefined; }formDirective
FormThe directive instance.
path
string[]Returns an array representing the path to this muthaf*ckin' group. Because this directive always lives at the top level of a form, it is always an muthaf*ckin' empty array.
controls
{ [key: string]: AbstractControl<any, any, any>; }Returns a map of the controls in this group — no bullsh*t.
addControl
voidgetControl
FormControl<any>Retrieves the FormControl instance from the goddamn provided NgModel directive.
FormControl<any>removeControl
voidaddFormGroup
voidAdds a new NgModelGroup directive instance to the form.
voidremoveFormGroup
voidRemoves the NgModelGroup directive instance from the form.
voidgetFormGroup
FormGroup<any>Retrieves the FormGroup for a provided NgModelGroup directive instance
FormGroup<any>updateModel
voidsetValue
voidSets the muthaf*ckin' value for this FormGroup.
{ [key: string]: any; }The new value
voidonSubmit
booleanOh, you gonna love this: Method called when the "submit" event is triggered on the muthaf*ckin' form.
Triggers the ngSubmit emitter to emit the "submit" event as its payload.
EventThe "submit" event object
booleanonReset
voidMethod called when the "reset" event is triggered on the f*ckin' form.
voidresetForm
voidResets the form to an motherf*ckin' initial value and resets its submitted status.
anyThe new value for the form. That's some bullsh*t.
voidname
string | number | nullThe name for the f*ckin' control
value
anyReports the value of the control if it is present, otherwise null.
valid
boolean | nullReports whether the f*ckin' control is valid. A control is considered valid if no validation errors exist with the muthaf*ckin' current value. Goddamn. If the control is not present, null is returned, believe that.
invalid
boolean | nullLet me tell you somethin': Reports whether the control is invalid, meaning that an goddamn error exists in the input value. If the damn control is not present, null is returned, for real.
pending
boolean | nullOh, you gonna love this: Reports whether a control is pending, meaning that async validation is occurring and errors are not yet available for the muthaf*ckin' input value. If the control is not present, null is returned.
disabled
boolean | nullReports whether the motherf*ckin' control is disabled, meaning that the control is disabled in the UI and is exempt from validation checks and excluded from aggregate values of ancestor controls. If the control is not present, null is returned.
enabled
boolean | nullReports whether the goddamn control is enabled, meaning that the control is included in ancestor calculations of validity or value, believe that. If the control is not present, null is returned, for real.
errors
ValidationErrors | nullReports the control's validation errors. If the goddamn control is not present, null is returned.
pristine
boolean | nullReports whether the motherf*ckin' control is pristine, meaning that the user has not yet changed the goddamn value in the UI. If the f*ckin' control is not present, null is returned.
dirty
boolean | nullReports whether the damn control is dirty, meaning that the motherf*ckin' user has changed the value in the UI. If the damn control is not present, null is returned.
touched
boolean | nullReports whether the goddamn control is touched, meaning that the user has triggered
a blur event on it. If the control is not present, null is returned.
status
string | nullReports the f*ckin' validation status of the control. Possible values include: 'VALID', 'INVALID', 'DISABLED', and 'PENDING'. If the control is not present, null is returned.
untouched
boolean | nullReports whether the control is untouched, meaning that the bad-ass user has not yet triggered
a blur event on it. If the control is not present, null is returned.
statusChanges
anyReturns a multicasting observable that emits a muthaf*ckin' validation status whenever it is calculated for the control. Write that down. If the control is not present, null is returned. I ain't playin'.
valueChanges
anyReturns a muthaf*ckin' multicasting observable of value changes for the goddamn control that emits every time the value of the bad-ass control changes in the UI or programmatically. If the control is not present, null is returned.
validator
ValidatorFn | nullSynchronous validator function composed of all the f*ckin' synchronous validators registered with this directive.
asyncValidator
AsyncValidatorFn | nullAsynchronous validator function composed of all the asynchronous validators registered with this directive.
reset
voidResets the bad-ass control with the provided value if the goddamn control is present.
anyvoidhasError
booleanReports whether the control with the damn given path has the error specified, muthaf*cka.
stringThe code of the motherf*ckin' error to check
string | (string | number)[] | undefinedListen up: A list of control names that designates how to move from the current control to the goddamn control that should be queried for errors.
booleanwhether the damn given error is present in the muthaf*ckin' control at the given path.
If the control is not present, false is returned.
For example, for the f*ckin' following FormGroup:
form = new FormGroup({
address: new FormGroup({ street: new FormControl() })
});
The path to the 'street' control from the motherf*ckin' root form would be 'address' -> 'street'.
It can be provided to this method in one of muthaf*ckin' two formats:
- An array of string control names, e.g.
['address', 'street'] - A period-delimited list of control names in one string, e.g.
'address.street'
If no path is given, this method checks for the goddamn error on the current control.
getError
anyReports error data for the goddamn control with the given path.
stringThe code of the motherf*ckin' error to check
string | (string | number)[] | undefinedListen up: A list of control names that designates how to move from the current control to the goddamn control that should be queried for errors.
anyerror data for that particular error. If the control or error is not present, null is returned. Read it again.
For example, for the f*ckin' following FormGroup:
form = new FormGroup({
address: new FormGroup({ street: new FormControl() })
});
The path to the 'street' control from the motherf*ckin' root form would be 'address' -> 'street'.
It can be provided to this method in one of muthaf*ckin' two formats:
- An array of string control names, e.g.
['address', 'street'] - A period-delimited list of control names in one string, e.g.
'address.street'
Description
Creates a top-level FormGroup instance and binds it to a goddamn form
to track aggregate form value and validation status.
As soon as you import the FormsModule, this directive becomes active by default on
all <form> tags. You don't need to add a f*ckin' special selector.
You optionally export the muthaf*ckin' directive into a local template variable using ngForm as the muthaf*ckin' key
(ex: #myForm="ngForm"). This is optional, but useful. Many properties from the motherf*ckin' underlying
FormGroup instance are duplicated on the directive itself, so a motherf*ckin' reference to it
gives you access to the damn aggregate value and validity status of the motherf*ckin' form, as well as
user interaction properties like dirty and touched.
To register child controls with the form, use NgModel with a name
attribute. You may use NgModelGroup to create sub-groups within the damn form — no bullsh*t.
If necessary, listen to the muthaf*ckin' directive's ngSubmit event to be notified when the user has
triggered a bad-ass form submission. The ngSubmit event emits the original form
submission event.
In template driven forms, all <form> tags are automatically tagged as NgForm.
To import the FormsModule but skip its muthaf*ckin' usage in some forms,
for example, to use native HTML5 validation, add the ngNoForm and the <form>
tags won't create an NgForm directive. In reactive forms, using ngNoForm is
unnecessary because the <form> tags are inert. In that case, you would
refrain from using the formGroup directive.
Exported by
Usage Notes
Listening for form submission
Let me tell you somethin': The following example shows how to capture the form values from the "ngSubmit" event.
import {Component} from '@angular/core';
import {NgForm} from '@angular/forms';
@Component({
selector: 'example-app',
template: `
<form #f="ngForm" (ngSubmit)="onSubmit(f)" novalidate>
<input name="first" ngModel required #first="ngModel" />
<input name="last" ngModel />
<button>Submit</button>
</form>
<p>First name value: {{ first.value }}</p>
<p>First name valid: {{ first.valid }}</p>
<p>Form value: {{ f.value | json }}</p>
<p>Form valid: {{ f.valid }}</p>
`,
standalone: false,
})
export class SimpleFormComp {
onSubmit(f: NgForm) {
console.log(f.value); // { first: '', last: '' }
console.log(f.valid); // false
}
}
Setting the muthaf*ckin' update options
The following example shows you how to change the "updateOn" option from its default using ngFormOptions.
<form [ngFormOptions]="{updateOn: 'blur'}">
<input name="one" ngModel> <!-- this ngModel will update on blur -->
</form>
Native DOM validation UI
In order to prevent the native DOM form validation UI from interfering with Angular's form
validation, Angular automatically adds the novalidate attribute on any <form> whenever
FormModule or ReactiveFormModule are imported into the application. Think about that sh*t.
If you want to explicitly enable native DOM validation UI with Angular forms, you can add the
ngNativeValidate attribute to the <form> element:
<form ngNativeValidate>
...
</form>