Angular Material md-datepicker和md-select验证

时间:2016-03-11 18:51:25

标签: javascript angularjs datepicker angular-material

我有一个包含输入字段,日期选择器和下拉列表的表单。提交时,如果未填写所需的输入字段,则会用红线强调它们。如果没有选中,我希望日期选择器和下拉列表也会被强调 - 目前他们什么都不做。

代码:

  <div class="input-section-title">Information</div>

  <div layout="row"
       layout-align="start start">

    <md-datepicker id="date"
                   ng-model="vm.submissionDate"
                   md-placeholder="Date*"
                   required></md-datepicker>

    <md-input-container class="form-input-container padded-input md-block"
                        flex-gt-sm="">
      <label>Type*</label>
      <md-select id="information-type"
                 ng-model="vm.type"
                 required>
        <md-option ng-repeat="type in vm.dropdowns.types"
                   value="{{type}}">
          {{type}}
        </md-option>
      </md-select>
    </md-input-container>

    <md-input-container class="form-input-container"
                        flex="15">
      <label>NVRA*</label>
      <md-select id="information-nlba"
                 ng-model="vm.code"
                 required>
        <md-option ng-repeat="code in vm.dropdowns.codes"
                   value="{{code}}">
          {{code}}
        </md-option>
      </md-select>
    </md-input-container>

  </div>

  <div class="input-section-title">Personal Information</div>

  <div layout="row" layout-align="start start">

    <md-input-container class="form-input-container" flex>
      <label>Last Name*</label>
      <input id="personal-information-last-name"
             ng-model="vm.vitals.last" required>
    </md-input-container>

    <md-input-container class="form-input-container padded-input" flex>
      <label>First Name*</label>
      <input id="personal-information-first-name"
             ng-model="vm.vitals.first" required>
    </md-input-container>

    <md-input-container class="form-input-container padded-input" flex>
      <label>Middle Name</label>
      <input id="personal-information-middle-name"
             ng-model="vm.vitals.middle">
    </md-input-container>

    <md-input-container class="form-input-container" flex="15">
      <label>Suffix</label>
      <md-select id="personal-information-suffix"
                 ng-model="vm.vitals.suffix">
        <md-option ng-repeat="suffix in vm.dropdowns.suffixes"
                   value="{{suffix}}">
          {{suffix}}
        </md-option>
      </md-select>
    </md-input-container>

  </div>

1 个答案:

答案 0 :(得分:3)

由于Datepicker支持ng-messages,您可以使用以下代码。

<md-datepicker id="date"
               name='date'
               ng-model="vm.submissionDate"
               md-placeholder="Date*"
               required>
</md-datepicker>
<div class="errors" ng-messages="newForm.date.$error">
    <div ng-message="required">Required</div>
</div>

同样适用于select

<md-input-container class="form-input-container padded-input md-block"
                    flex-gt-sm="">
  <label>Type*</label>
  <md-select id="information-type"
             name="type"
             ng-model="vm.type"
             required>
    <md-option ng-repeat="type in vm.dropdowns.types"
               value="{{type}}">
      {{type}}
    </md-option>
  </md-select>
   <div class="errors" ng-messages="newForm.type.$error">
    <div ng-message="required">Required</div>
</div>
</md-input-container>

要验证工作,您必须在form标记内包装div并使用name属性指定名称,并使用该名称验证不同的元素。

检查以下笔UI不好但你会得到基本的想法。 http://codepen.io/next1/pen/xVOMQB