feat(forms): handle string with and without line boundary on pattern validator (#19256)
PR Close #19256
This commit is contained in:
parent
d6ba9f9fb4
commit
54bf179888
|
@ -163,7 +163,14 @@ export class Validators {
|
||||||
let regex: RegExp;
|
let regex: RegExp;
|
||||||
let regexStr: string;
|
let regexStr: string;
|
||||||
if (typeof pattern === 'string') {
|
if (typeof pattern === 'string') {
|
||||||
regexStr = `^${pattern}$`;
|
regexStr = '';
|
||||||
|
|
||||||
|
if (pattern.charAt(0) !== '^') regexStr += '^';
|
||||||
|
|
||||||
|
regexStr += pattern;
|
||||||
|
|
||||||
|
if (pattern.charAt(pattern.length - 1) !== '$') regexStr += '$';
|
||||||
|
|
||||||
regex = new RegExp(regexStr);
|
regex = new RegExp(regexStr);
|
||||||
} else {
|
} else {
|
||||||
regexStr = pattern.toString();
|
regexStr = pattern.toString();
|
||||||
|
|
|
@ -270,6 +270,12 @@ import {map} from 'rxjs/operator/map';
|
||||||
|
|
||||||
it('should not error on "undefined" pattern',
|
it('should not error on "undefined" pattern',
|
||||||
() => expect(Validators.pattern(undefined !)(new FormControl('aaAA'))).toBeNull());
|
() => expect(Validators.pattern(undefined !)(new FormControl('aaAA'))).toBeNull());
|
||||||
|
|
||||||
|
it('should work with string containing line boundary',
|
||||||
|
() => expect(Validators.pattern('^[aA]*$')(new FormControl('aaAA'))).toBeNull());
|
||||||
|
|
||||||
|
it('should work with string not containing line boundary',
|
||||||
|
() => expect(Validators.pattern('[aA]*')(new FormControl('aaAA'))).toBeNull());
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('compose', () => {
|
describe('compose', () => {
|
||||||
|
|
Loading…
Reference in New Issue