简体   繁体   English

角材料md-datepicker和md-select验证

[英]Angular Material md-datepicker and md-select validation

I have a form with input fields, datepickers, and dropdowns. 我有一个带有输入字段,日期选择器和下拉列表的表单。 On submit, if the required input fields are not filled out, they are underscored with a red line. 提交时,如果未填写必填项,则用红线突出显示。 I would like the datepickers and dropdowns to also be underscored if not selected - at the moment they do nothing. 我希望datepickers和下拉列表也能被强调(如果未选中的话)-目前它们什么也不做。

Code: 码:

  <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>

As Datepicker supports ng-messages you can use the following code. 由于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>

and same goes for 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>

For Validation to work you must wrap you div within form tag and assign name using name attribute and use that name for validation of different elements. 为了使验证生效,您必须将div包裹在form标记中,并使用name属性分配名称,并将该名称用于不同元素的验证。

check the following pen UI is not good but you will get the basic idea. 检查以下手写笔UI不好,但是您会得到基本的想法。 http://codepen.io/next1/pen/xVOMQB http://codepen.io/next1/pen/xVOMQB

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM