fix(forms): properly validate empty strings with patterns (#11450)
This commit is contained in:
parent
43923ffcf5
commit
4a57dcfd8d
@ -96,7 +96,6 @@ export class Validators {
|
|||||||
*/
|
*/
|
||||||
static pattern(pattern: string): ValidatorFn {
|
static pattern(pattern: string): ValidatorFn {
|
||||||
return (control: AbstractControl): {[key: string]: any} => {
|
return (control: AbstractControl): {[key: string]: any} => {
|
||||||
if (isPresent(Validators.required(control))) return null;
|
|
||||||
let regex = new RegExp(`^${pattern}$`);
|
let regex = new RegExp(`^${pattern}$`);
|
||||||
let v: string = control.value;
|
let v: string = control.value;
|
||||||
return regex.test(v) ? null :
|
return regex.test(v) ? null :
|
||||||
|
@ -91,6 +91,9 @@ export function main() {
|
|||||||
it('should not error on null',
|
it('should not error on null',
|
||||||
() => { expect(Validators.pattern('[a-zA-Z ]*')(new FormControl(null))).toEqual(null); });
|
() => { expect(Validators.pattern('[a-zA-Z ]*')(new FormControl(null))).toEqual(null); });
|
||||||
|
|
||||||
|
it('should not error on null value and "null" pattern',
|
||||||
|
() => { expect(Validators.pattern('null')(new FormControl(null))).toEqual(null); });
|
||||||
|
|
||||||
it('should not error on valid strings', () => {
|
it('should not error on valid strings', () => {
|
||||||
expect(Validators.pattern('[a-zA-Z ]*')(new FormControl('aaAA'))).toEqual(null);
|
expect(Validators.pattern('[a-zA-Z ]*')(new FormControl('aaAA'))).toEqual(null);
|
||||||
});
|
});
|
||||||
@ -100,6 +103,12 @@ export function main() {
|
|||||||
'pattern': {'requiredPattern': '^[a-zA-Z ]*$', 'actualValue': 'aaa0'}
|
'pattern': {'requiredPattern': '^[a-zA-Z ]*$', 'actualValue': 'aaa0'}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should error on failure to match empty string', () => {
|
||||||
|
expect(Validators.pattern('[a-zA-Z]+')(new FormControl(''))).toEqual({
|
||||||
|
'pattern': {'requiredPattern': '^[a-zA-Z]+$', 'actualValue': ''}
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('compose', () => {
|
describe('compose', () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user