[Forms] Support patterns

Support patterns for text fields, using the ng-pattern
directive. WTD-530.
This commit is contained in:
Victor Woeltjen 2014-11-28 14:08:33 -08:00
parent aaa0ff768d
commit 354327accc
3 changed files with 47 additions and 12 deletions

View File

@ -3,6 +3,7 @@
<input type="text" <input type="text"
ng-required="ngRequired" ng-required="ngRequired"
ng-model="ngModel[field]" ng-model="ngModel[field]"
ng-pattern="ngPattern"
name="mctControl"> name="mctControl">
</span> </span>
</span> </span>

View File

@ -23,23 +23,26 @@
</div> </div>
<div class='controls'> <div class='controls'>
<div class="wrapper" ng-if="row.control"> <div class="wrapper" ng-if="row.control">
<mct-control key="row.control" <mct-control key="row.control"
ng-model="ngModel" ng-model="ngModel"
ng-required="row.required" ng-required="row.required"
ng-pattern="getRegExp(row.pattern)"
options="row.options" options="row.options"
structure="row" structure="row"
field="row.key"> field="row.key">
</mct-control> </mct-control>
</div> </div>
<div ng-repeat="item in row.items" class="validates"> <div ng-repeat="item in row.items" class="validates">
<ng-form name="mctFormInner"> <ng-form name="mctFormItem">
<mct-control key="item.control" <mct-control key="item.control"
ng-model="ngModel" ng-model="ngModel"
ng-required="item.required" ng-required="item.required"
ng-pattern="getRegExp(item.pattern)"
options="item.options" options="item.options"
structure="item"> structure="row"
field="item.key">
</mct-control> </mct-control>
{{item.name}}
</ng-form> </ng-form>
</div> </div>
</div> </div>

View File

@ -8,6 +8,8 @@ define(
function () { function () {
"use strict"; "use strict";
var MATCH_ALL = /^.*$/;
/** /**
* *
* @constructor * @constructor
@ -20,18 +22,47 @@ define(
].join("/"); ].join("/");
function controller($scope) { function controller($scope) {
var regexps = [],
matchAll = /.*/;
// ng-pattern seems to want a RegExp, and not a
// string (despite what documentation says) but
// we want form structure to be JSON-expressible,
// so we make RegExp's from strings as-needed
function getRegExp(pattern) {
// If undefined, don't apply a pattern
if (!pattern) {
return MATCH_ALL;
}
// Just echo if it's already a regexp
if (pattern instanceof RegExp) {
return pattern;
}
// Otherwise, assume a string
// Cache for easy lookup later (so we don't
// creat a new RegExp every digest cycle)
if (!regexps[pattern]) {
regexps[pattern] = new RegExp(pattern);
}
return regexps[pattern];
}
$scope.$watch("mctForm", function (mctForm) { $scope.$watch("mctForm", function (mctForm) {
console.log(JSON.stringify(mctForm));
if ($scope.name) { if ($scope.name) {
$scope.$parent[$scope.name] = mctForm; $scope.$parent[$scope.name] = mctForm;
} }
}); });
$scope.getRegExp = getRegExp;
} }
return { return {
restrict: "E", restrict: "E",
templateUrl: templatePath, templateUrl: templatePath,
link: controller, controller: controller,
scope: { scope: {
structure: "=", structure: "=",
ngModel: "=", ngModel: "=",