2

I am doing error validation in Angular as follows.

<select ng-model = "$parent.color" class = "form-control"
    ng-options = "color as color for color in $parent.colors" required>
    <option value="">Choose an option</option> 
</select>

<span ng-show="serviceForm.$parent.color.$error.required"> My custom error message </span>

The error validation message never shows up. How to fix this?

2 Answers 2

2

Did you include the ngRequire module ?

ngRequire

<script>
angular.module('ngRequiredExample', [])
.controller('ExampleController', ['$scope', function($scope) {
  $scope.required = true;
}]);
</script>
<div ng-controller="ExampleController">
 <form name="form">
 <label for="required">Toggle required: </label>
 <input type="checkbox" ng-model="required" id="required" />
 <br>
 <label for="input">This input must be filled if `required` is true:</label>
 <input type="text" ng-model="model" id="input" name="input" ng-required="required" /> <br>
 <hr>
 required error set? = <code>{{form.input.$error.required}}</code><br>
 model = <code>{{model}}</code>

Try this if it works.

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

Comments

0

Your code can be like to this to work properly.

<form name="form1">
   <select name="select" ng-model = "$parent.color" class = "form-control"
       ng-options = "color as color for color in $parent.colors" required>
       <option value="">Choose an option</option>    
   </select>
   <span ng-show="form1.select.$error.required"> My custom error message </span>
</form>

You are not flowing FormController. View this link

2 Comments

is there a way to get the message to only show on submit? Right now it shows always
Yes you can use a scope variable as a flag of submit raise it when submit is called else make it false. and use it in ng-show condition as well with && operator. Ex $scope.submitted

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.