我有一个包含 2 个字段、邮政编码和国家/地区的表格。我需要根据国家选择验证邮政编码,使用 $validators
这是 ngController 代码
$scope.schema = {
"type": "object",
"title": "my form",
"properties": {
"postalCode": {
"title": "Postal Code",
"type": "string"
},
"countryCode": {
"title": "Country",
"type": "string",
"enum": [
"CA",
"US"
]
}
},
"required": [
"countryCode"
]
};
$scope.form = [{
"key": "postalCode",
$validators: {
// I would like to pass countryCode as the argument but doesn't work because I don't have model in my closure
postalCodeRegex: function (countryCodeValue) {
if (angular.isString(countryCodeValue)) {
var canada = "^(?!.*[DFIOQU])[A-VXY][0-9][A-Z]●?[0-9][A-Z][0-9]$";
if (/canada/.test(countryCodeValue)) return true;
else return false;
}
// if value is empty
return true;
}
}
},
"countryCode", {
"type": "submit",
"style": "btn-info",
"title": "OK"
}
];
是否有另一种方法可以从 $validators 逻辑访问模型或范围?
plunker 链接以查看我的代码: http ://plnkr.co/edit/DDbMiw?p=info