0

How do I get the errors class/message to display using angular js and forms etc?

I tried this but it doesn't seem to validate at all:

<div class="form-group" ng-class="{'has-error': obj.title.$invalid}">
  <label for="name">Name</label>
  <input type="text" class="form-control" id="name" ng-model="obj.title" required>
</div>
<div class="alert alert-danger" ng-show="obj.title.$invalid">
  You are required to enter a name.
</div>

http://plnkr.co/edit/C6eU4pIS8FTfA59SCShh?p=preview

1 Answer 1

2

You have to wrap it inside a <form>, give the form a name and the input a name, then access the validity though formName.inputName

<form name="form">
    <div class="form-group" ng-class="{'has-error': obj.title.$invalid}">
      <label for="name">Name</label>
      <input type="text" name="title" class="form-control" id="name" ng-model="obj.title" required>
    </div>
    <div class="alert alert-danger" ng-show="form.title.$invalid">
      You are required to enter a name.
    </div>
  </form>

Be aware that <form> is an instance of FormController in angular. That's where we have angular validation.

DEMO

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.