如何验证具有必需属性的paper-radio-group元素

时间:2016-06-08 23:16:43

标签: javascript jquery polymer

这是与this question类似的问题,但没有得到回答&是1.0之前 我需要确保在表单提交上至少选择一个paper-radio-button 我可以为每个paper-radio-button放置一个必需的属性,但这意味着必须选择每个单选按钮。

<iron-label> Type<br/>
    <paper-radio-group name="types" allow-empty-selection attr-for-selected="value" selected="{{model.type}}" noink >
        <paper-radio-button name="type" value="home" required> Home</paper-radio-button>
        <paper-radio-button name="type" value="work" required> Work</paper-radio-button>
        <paper-radio-button name="type" value="portal" required> Portal</paper-radio-button>
        <paper-radio-button name="type" value="shipping" required> Shipping</paper-radio-button>
        <paper-radio-button name="type" value="billing" required> Billing</paper-radio-button>
        <paper-radio-button name="type" value="other" required> Other</paper-radio-button>
    </paper-radio-group>
</iron-label>

1 个答案:

答案 0 :(得分:1)

不是给用户选择不选择任何单选按钮,而是强制选择。您可以通过从paper-radio-group移除allow-empty-selection和从required移除paper-radio-button属性来执行此操作。我还建议在paper-radio-group上使用fallback-selection,以便model.type中的意外值会导致默认值,例如“其他”。

<head>
  <base href="https://polygit.org/polymer+1.11.0/components/">
  <script src="webcomponentsjs/webcomponents-lite.js"></script>
  <link rel="import" href="polymer/polymer.html">
  <link rel="import" href="iron-form/iron-form.html">
  <link rel="import" href="iron-label/iron-label.html">
  <link rel="import" href="paper-radio-group/paper-radio-group.html">
  <link rel="import" href="paper-radio-button/paper-radio-button.html">
  <link rel="import" href="paper-button/paper-button.html">
</head>
<body>
  <x-form></x-form>

  <dom-module id="x-form">
    <template>
      <iron-form id="form">
        <form action="">
          <iron-label> Type<br/>
            <paper-radio-group name="types"  attr-for-selected="value" fallback-selection="other" selected="{{model.type}}" noink>
              <paper-radio-button name="type" value="home"> Home</paper-radio-button>
              <paper-radio-button name="type" value="work"> Work</paper-radio-button>
              <paper-radio-button name="type" value="portal"> Portal</paper-radio-button>
              <paper-radio-button name="type" value="shipping"> Shipping</paper-radio-button>
              <paper-radio-button name="type" value="billing"> Billing</paper-radio-button>
              <paper-radio-button name="type" value="other"> Other</paper-radio-button>
            </paper-radio-group>
          </iron-label>
          <div>
            <paper-button on-click="submit">Submit</paper-button>
          </div>
        </form>
      </iron-form>
    </template>
    <script>
      HTMLImports.whenReady(function() {
        Polymer({
          is: 'x-form',
          properties : {
            model: {
              type: Object,
              value: function() { return {type: 'fake'}; }
            }
          },
          submit: function() {
            console.log('type', this.model.type);
          }
        });
      });
    </script>
  </dom-module>
</body>

codepen