本文整理匯總了TypeScript中@angular/forms.FormControl.setErrors方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript FormControl.setErrors方法的具體用法?TypeScript FormControl.setErrors怎麽用?TypeScript FormControl.setErrors使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類@angular/forms.FormControl
的用法示例。
在下文中一共展示了FormControl.setErrors方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: it
it('should unset error when values are equal', () => {
y.setErrors({ match: true });
CdValidators.match('x', 'y')(form);
expect(x.hasError('match')).toBeFalsy();
expect(y.hasError('match')).toBeFalsy();
expect(y.valid).toBeTruthy();
});
示例2: it
it('should work for control with error', () => {
let control: FormControl = new FormControl('');
control.setErrors({ oldError: 'test', newError: 'test' });
let validated = AbstractControlUtil.removeError(control, 'newError');
expect(control.hasError('newError')).toBeFalsy();
expect(control.hasError('oldError')).toBeTruthy();
});
示例3: it
it('should reset the errors and validity when the value changes', () => {
const c = new FormControl('someValue', Validators.required);
c.setErrors({'someError': true});
c.setValue('');
expect(c.errors).toEqual({'required': true});
});
示例4: it
it("should create error messages", () => {
let errorMessages,
testControl: FormControl = new FormControl(),
testModel: DynamicFormControlModel = new DynamicInputModel({
id: "testModel",
minLength: 5,
errorMessages: {
required: "Field is required",
minLength: "Field must contain at least {{ minLength }} characters",
custom1: "Field {{ id }} has a custom error",
custom2: "Field has a custom error: {{ validator.param }}"
}
});
errorMessages = service.createErrorMessages(testControl, testModel);
expect(errorMessages.length).toBe(0);
testControl.setErrors({required: true, minlength: 5});
errorMessages = service.createErrorMessages(testControl, testModel);
expect(errorMessages.length).toBe(2);
expect(errorMessages[0]).toEqual((testModel.errorMessages as any)["required"]);
expect(errorMessages[1]).toEqual("Field must contain at least 5 characters");
testControl.setErrors({custom1: true});
errorMessages = service.createErrorMessages(testControl, testModel);
expect(errorMessages.length).toBe(1);
expect(errorMessages[0]).toEqual(`Field ${testModel.id} has a custom error`);
testControl.setErrors({custom2: {param: 42}});
errorMessages = service.createErrorMessages(testControl, testModel);
expect(errorMessages.length).toBe(1);
expect(errorMessages[0]).toEqual("Field has a custom error: 42");
});
示例5: it
it('should keep other existing errors', () => {
y.setErrors({ match: true, notUnique: true });
CdValidators.match('x', 'y')(form);
formHelper.expectValid('x');
formHelper.expectError('y', 'notUnique');
});