I have a Form with 2 fields, postal code and country. I need to validate postal code depending on country selection, using $validators
Here is the ngController code
$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"
}
];
is there another way to access model or scope from the $validators logic?
plunker link to see my code: http://plnkr.co/edit/DDbMiw?p=info
@houss You could use onChange to combine the values into a new value "US-90210", saved within a hidden field on a key added as part of the form, then validate on that hidden field with a regex covering both formats. Alternatively if you may need more than a few countries then I would use a directive which can be added via the add-on mechanisms. Like the datepicker example but in the template add a component to do the heavy lifting
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.