本文整理汇总了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');
});