26

I have a text input field for address, whenever the address is entered and search button next to the input field is clicked, I geocode the address and replace the input text with the result from the geocoder. I also set a scope variable 'addressOk'.

is it possible to invalidate this specific form input field according to $scope.addressOk or do I have to write a directive for this address geocoding input that invalidates the form when needed ?

5
  • What do you mean by invalidate? Commented Jan 23, 2013 at 11:34
  • 1
    I mean that to mark that field as invalid so the form can't be submitted Commented Jan 23, 2013 at 12:25
  • jsfiddle.net/sunnycpp/8dt94/23 Looks like, It is not possible, Please post this question on mailing-list. Commented Jan 23, 2013 at 15:36
  • github.com/angular/angular.js/blob/master/src/ng/directive/… Checking this out can help too. Commented Jan 23, 2013 at 15:39
  • I found the solution myself and posted the answer Commented Jan 23, 2013 at 15:59

1 Answer 1

41

I found the solution, add a name attribute to the <form> and <input>, and then in the controller its possible to do $scope.formName.inputName.$setValidity(errorKey, isValid).

Then, its possible to display errors with something like ng-show="formName.inputName.$error.errorkey"

according to http://docs.angularjs.org/api/ng.directive:form If name attribute is specified, the form controller is published onto the current scope under this name.

Sign up to request clarification or add additional context in comments.

3 Comments

What if inputNameis a variable?
@lightalex justuse the object notation ;) $scope.formName[inputName].$setValidity(...)
After you have added a custom boolean property to yourFieldName.$error object via $setValidity, you can use AngularJS ngMessages to display the appropriate error message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.