I have a directive that queries google maps api with the form field value (event_postcode). It needs to be a required field. Yet when the field is populated, and maps api (via manageEventsSrv.getPostcodeLocation()) returns a valid address, the forms submit button remains disabled, i.e. form is invalid.
My directive:
app.directive('geoLocatePostcode', ['manageEventsSrv', function(manageEventsSrv) {
return {
restrict: 'A',
require: 'ngModel',
link : function(scope, elem, attrs, ctrl){
var valid = false;
ctrl.$parsers.unshift(function(value) {
if(value.length>=3){
manageEventsSrv.getPostcodeLocation(value+',uk').then(function(result){
valid = true;
scope.point = new Parse.GeoPoint({latitude:result.lat,longitude:result.lng}); // parse.com geopoint
ctrl.$setValidity('locationNotFound', true);
}, function(result){
valid = false;
ctrl.$setValidity('locationNotFound', false);
});
} else {
ctrl.$setValidity('locationNotFound', true);
valid = false;
}
return valid ? value : undefined;
});
}
}
}]);
Form field:
<input type="text" placeholder="First 4 Characters Only" ng-model="event_postcode" maxlength="4" name="event_postcode" required geo-locate-postcode>
<span data-ng-show="createEventForm.event_postcode.$error.locationNotFound">Location not found</span>
<button type="submit" class="button" ng-disabled="createEventForm.$invalid">Create</button>
I assume i am not implementing "required" properly when using a custom directive.
Think i might have got to the bottom of this.... i needed to change
return valid ? value : undefined;
to return valid ? value : false;