2

I got this form:

<form ng-controller="SomeCtrl as some" name="product[[product.id]]" ng-submit="some.addToCart(something, product[[product.id]].$valid)" novalidate></form>

Then html looks like:

<form ng-controller="SomeCtrl as some" name="product1" ng-submit="some.addToCart(something, product1.$valid)" novalidate=""></form>

Controller:

this.addToCart = function(something, isValid) {
    console.log(isValid);
}

isValid is always undefined. How to detect if form is valid in controller?

1
  • Nope, in that example it's disabling submit button, but I don't want to do that, I want to check in controller $valid status Commented Apr 28, 2015 at 21:24

1 Answer 1

5

Demo You don't want to use ng-submit if you want to do your own validation in the controller because it will block the submission of the form if it is invalid.

Just use a regular button with a function in ng-click that checks the condition of the form.

Controller:

  $scope.submit = function() {
    console.log($scope.myForm.$valid)
  }

HTML

<form name="myForm">
  <input ng-model="myForm.text" type="text" required />
  <button ng-click="submit()">Submit</button>
</form>
Sign up to request clarification or add additional context in comments.

Comments

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.