Search code examples
javascriptangularjsui-select2ui-selectng-messages

Form validation with error message using angular ngMessage


I am using ui-select with some multi select field and I want to implement a client-side <form> validation like this :

<form name="newProjectForm" ng-submit="p.addProject()" class="inputsContainer" novalidate> 
  <div class="form-group" ng-class="{ 'has-error' : newProjectForm.frameworks.$invalid && 
                                                    !newProjectForm.frameworks.$pristine }">
    <label>Frameworks</label>
  <ui-select name="frameworks" multiple ng-model="p.newProject.frameworks" sortable="true" 
             close-on-select="false" required>
    <ui-select-match placeholder="Select framework...">{{$item.name}}</ui-select-match>
    <ui-select-choices repeat="framework.id as framework in p.frameworksList
                               | propsFilter: {name: $select.search, language: $select.search}">
      <div ng-bind-html="framework.name | highlight: $select.search"></div>
      <small>
        language : <span ng-bind-html="''+framework.language | highlight: $select.search"></span>
      </small>
    </ui-select-choices>
  </ui-select>
    <div ng-messages="newProjectForm.frameworks.$error">
      <p ng-messages="required">THIS IS REQUIRED</p>
    </div>
    <p ng-show="newProjectForm.frameworks.$invalid && !newProjectForm.frameworks.$pristine" 
       class="help-block">Project frameworks is required.</p>
  </div>
<!-- ... -->

So, the problem is that the conditions I tried are not working :

newProjectForm.frameworks.$invalid && !newProjectForm.frameworks.$pristine"> seems to be always false.

<div ng-messages="newProjectForm.frameworks.$error"> doesnt seem to work as well.

However, I tested using this line instead :

<div class="form-group" ng-class="{ 'has-error' : true }">

And it worked, so I'm guessing that's the conditions that are not working.


Solution

  • Ok, so it looks like ui-select and required don't work together.

    So if you meet the same issue, you can use a custom directive instead of required :

    app.directive('uiSelectRequired', function() {
      return {
        require: 'ngModel',
        link: function(scope, elm, attrs, ctrl) {
          ctrl.$validators.uiSelectRequired = function(modelValue, viewValue) {
            if (modelValue && modelValue.length > 0)
              return true;
            return false;
          };
        }
      };
    });
    

    Then you just need to replace required with ui-select-required in the HTML view.