How to validate form in AngularJS 1.x component

删除回忆录丶 提交于 2020-02-04 11:43:46

问题


I have a form that its fields are inside the component. Now I want to validate the fields. The problem is, my messages don't show up. also the pattern doesn't have any effects. I tried to fix it with help of this post, but no success.

Does someone have any idea how can I show my messages?

index.html

  <form name="dataForm">
    <md-tabs>
      <valid-data form-reference="dataForm"></valid-data>
    </md-tabs>

    <div>
      <md-button type="submit" ng-click="vm.save()"> Save </md-button>
    </div>
  </form>

component.html

<md-tab label="Info Data">
  <md-content class="md-margin">

    <div layout-gt-sm="row">
      <md-input-container class="md-block" flex-gt-sm>
        <label>Name</label>
        <input ng-model="vm.data.name" name="name" required
               ng-minlength="10"
               ng-maxlength="20"
               pattern="{{validation.validateWord}}">
        <div ng-messages="formReference.name.$error" ng-if="formReference.name.$touched">
          <div ng-message="required">Name is required.</div>
          <div ng-message="minlength">minLength Error msg.</div>
          <div ng-message="maxlength">maxLength Error msg.</div>
          <div ng-message="pattern">Invalid characters.</div>
        </div>
      </md-input-container>
    </div>
  </md-content>
</md-tab>

This is the pattern:

$rootScope.validation = {
    'validateWord': '^\\w+(-\\w+)*$', // 0-9, a-z, A-Z, underscores + dashes
  };

component.controller.js

bindings: {
    formReference: '<'
  }

来源:https://stackoverflow.com/questions/45593764/how-to-validate-form-in-angularjs-1-x-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!