feat(forms): handle string with and without line boundary on pattern validator (#19256)

PR Close #19256
This commit is contained in:
ghetolay 2017-09-18 22:28:36 +02:00 committed by Alex Eagle
parent d6ba9f9fb4
commit 54bf179888
2 changed files with 14 additions and 1 deletions

View File

@ -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();

View File

@ -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', () => {