angularjsng-messages

How to display a message when a form input is valid?


I'm having a hard time to display a message when an input is valid. To display error messages, it is working alright. This is what I'm trying:

<form>
...
<div class="form-group">
                    <label class="col-xs-2 control-label" for="inputSuccess">Username</label>
                    <div class="col-xs-10">
                        <input type="text" id="inputUser" name="usernameInput" class="form-control textbox" 
                               placeholder="Digite seu Username" width="25" ng-model="username.value" 
                               ng-minlength="4" ng-maxlength="10" valid="validateForm.usernameInput.$valid" required>
                        <div ng-if="validateForm.usernameInput.$dirty" ng-messages="validateForm.usernameInput.$error">
                            <div class="error-message" ng-message="required">*This field is required</div>
                            <div class="error-message" ng-message="minlength">*Username too short</div>
                            <div class="error-message" ng-message="maxlength">*Username too long</div>
                        </div>
                        <div ng-if="validateForm.usernameInput.$dirty" ng-messages="validateForm.usernameInput.$valid">
                            <div class="valid-message" ng-message="valid">valid!</div>
                        </div>
                    </div>
                </div>

</form

I also tried to set "ng-message" with the $valid ng-model but to no avail. Any tips?


Solution

  • TLDR

    angular.module('formDemo', ['ngMessages']);
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.20/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.20/angular-messages.js"></script>
    
    <body ng-app="formDemo">
      <form name="validateForm">
        ...
        <div class="form-group">
          <label class="col-xs-2 control-label" for="inputSuccess">Username</label>
          <div class="col-xs-10">
            <input 
            type="text" 
            id="inputUser"
            name="usernameInput"
            class="form-control textbox"
            placeholder="Digite seu Username"
            width="25"
            ng-model="username.value"
            ng-minlength="4"
            ng-maxlength="10"
            required>
            <div ng-if="validateForm.usernameInput.$dirty" ng-messages="validateForm.usernameInput.$error">
              <div class="error-message" ng-message="required">*This field is required</div>
              <div class="error-message" ng-message="minlength">*Username too short</div>
              <div class="error-message" ng-message="maxlength">*Username too long</div>
            </div>
            <div ng-if="validateForm.usernameInput.$dirty" ng-messages="validateForm.usernameInput">
              <div class="valid-message" ng-message="$valid">valid!</div>
            </div>
          </div>
        </div>
    
      </form>
    </body>