0

CODE:

view.html

<form name="vm.form.articleForm" class="form-horizontal" ng-submit="vm.save(vm.form.articleForm.$valid)" novalidate>
  <fieldset>
    <div class="form-group" show-errors>
      <label class="control-label" for="title">Title</label>
      <input name="title" type="text" ng-model="vm.article.title" id="title" class="form-control" placeholder="Title" required autofocus>
      <div ng-messages="vm.form.articleForm.title.$error" role="alert">
        <p class="help-block error-text" ng-message="required">Article title is required.</p>
      </div>
    </div>
    <div class="form-group">
      <label class="control-label" for="content">Content</label>
      <textarea name="content" data-ng-model="vm.article.content" id="content" class="form-control" cols="30" rows="10" placeholder="Min: 1400 characters. Max: 14000 characters." required  ng-maxlength="14000" highlight-on-error></textarea>
        <!--counter-->
      <br>
      <span class="form-help">{{vm.article.content.length}} Characters</span>
      <div ng-messages="vm.form.articleForm.content.$error" role="alert">
        <p class="help-block error-text" ng-message="required">min: 1400 characters max: 14000</p>
      </div>
    </div>
    <div class="form-group">
      <button type="submit" class="btn btn-default">{{vm.article._id ? 'Update' : 'Create'}}</button>
    </div>
  </fieldset>
</form>

controller

(function () {
  'use strict';

  angular
    .module('articles')
    .controller('ArticlesCreateController', ArticlesCreateController);

  ArticlesCreateController.$inject = ['$scope', '$state', '$window', 'articleResolve', 'Authentication', 'Notification'];

  function ArticlesCreateController($scope, $state, $window, article, Authentication, Notification) {
    var vm = this;

    vm.article = article;
    vm.authentication = Authentication;
    vm.form = {};
    vm.remove = remove;
    vm.save = save;

    // Remove existing Article
    function remove() {
      if ($window.confirm('Are you sure you want to delete?')) {
        vm.article.$remove(function() {
          $state.go('articles.list');
          Notification.success({ message: '<i class="glyphicon glyphicon-ok"></i> Article deleted successfully!' });
        });
      }
    }

    // Save Article
    function save(isValid) {
      if (!isValid) {
        $scope.$broadcast('show-errors-check-validity', 'vm.form.articleForm');
        return false;
      }

      // Create a new article, or update the current instance
      vm.article.createOrUpdate()
        .then(successCallback)
        .catch(errorCallback);

      function successCallback(res) {
        $state.go('articles.list'); // should we send the User to the list or the updated Article's view?
        Notification.success({ message: '<i class="glyphicon glyphicon-ok"></i> Article saved successfully!' });
      }

      function errorCallback(res) {
        Notification.error({ message: res.data.message, title: '<i class="glyphicon glyphicon-remove"></i> Article save error!' });
      }
    }
  }
}());

SITUATION:

When I click submit with an empty textarea, the error message "min: 1400 characters max: 14000 characters" does not appear.

Currently using mean.js and customising it to learn the MEAN stack.


HOW TO REPRODUCE:

Download mean.js: https://github.com/meanjs/mean

File is the following: modules/articles/client/views/admin/form-article.client.view.html

Coder1000
  • 4,071
  • 9
  • 35
  • 84

1 Answers1

1

You need to add the message type for maxlength validation.

Currently a message will only be displayed when you don't have any text because of the required flag, but once you hit max length you wont see the message.

<div ng-messages="vm.form.articleForm.content.$error" role="alert">
        <p class="help-block error-text" ng-message="required">min: 1400 characters max: 14000</p>
</div>

Add the following for a message.

<div ng-messages="vm.form.articleForm.content.$error" role="alert">
        <p class="help-block error-text" ng-message="required">min: 1400 characters max: 14000</p>
        <p class="help-block error-text" ng-message="maxlength">min: 1400 characters max: 14000</p>
      </div>

Your error message is also confusing as you have not set a minlength but specifying a min length error message.

If you are still having issues try to change it from ng-maxlength="14000" to just maxlength="14000" and also ensure you have ngMessages injected correctly into your application.

Robert Leggett
  • 972
  • 1
  • 8
  • 23
  • The issue is actually that I don't see any message when there is NO text :/ – Coder1000 Feb 06 '17 at 10:45
  • I don't have minlength because of this issue: http://stackoverflow.com/questions/42023145/how-to-create-a-character-counter-for-textarea-angular `minlength` intereferes with `ng-model`. – Coder1000 Feb 06 '17 at 10:47
  • It's fine actually, I moved my validation to the server :) – Coder1000 Feb 06 '17 at 11:02
  • The server should always have the validation anyways since front end validation can always be bypassed, if I can think in anything else you may be missing I'll update via a comment. – Robert Leggett Feb 06 '17 at 20:38
  • Try the following examples from here: https://scotch.io/tutorials/angularjs-form-validation-with-ngmessages and https://www.sitepoint.com/easy-form-validation-angularjs-ngmessages/ – Robert Leggett Feb 06 '17 at 20:43