最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I disable a button in polymer until a field is valid? - Stack Overflow

programmeradmin3浏览0评论

I like to create a basic dialog with polymer. The user shall enter a category name and if he entered something he can press on a button to save/send it. The button should be disabled as long as there is no value in the category field or as long as there is a validation error in the category field.

I tried this but it does not set the button to disabled initially.

<paper-input required auto-validate invalid="{{invalid}}"></paper-input>
<paper-button disabled$="[[invalid]]">Send</paper-button>

Polymer({
    is: 'list',
    properties: {  
        invalid: {
            type: Boolean
        },
        ...

If I use the function validate() on the input to pute the button value it will also trigger the invalid state (error msg) of the input although the user might not have entered something yet.

How can I get around this?

Reference: paper-input; paper-button

Maria's proposal with iron-form:

<form is="iron-form" method="get" action="/" id="eventsDemo">
  <paper-input name="name" label="Name" required auto-validate></paper-input>
  <paper-button raised disabled id="eventsDemoSubmit">
</form>

<script>
  eventsDemo.addEventListener('change', function(event) {
    // Validate the entire form to see if we should enable the `Submit` button.
    eventsDemoSubmit.disabled = !eventsDemo.validate();
  });
  document.getElementById('eventsDemo').addEventListener('iron-form-submit', function(event) {
    eventsDemoSubmit.disabled = false;
  });
  ...

I like to create a basic dialog with polymer. The user shall enter a category name and if he entered something he can press on a button to save/send it. The button should be disabled as long as there is no value in the category field or as long as there is a validation error in the category field.

I tried this but it does not set the button to disabled initially.

<paper-input required auto-validate invalid="{{invalid}}"></paper-input>
<paper-button disabled$="[[invalid]]">Send</paper-button>

Polymer({
    is: 'list',
    properties: {  
        invalid: {
            type: Boolean
        },
        ...

If I use the function validate() on the input to pute the button value it will also trigger the invalid state (error msg) of the input although the user might not have entered something yet.

How can I get around this?

Reference: paper-input; paper-button

Maria's proposal with iron-form:

<form is="iron-form" method="get" action="/" id="eventsDemo">
  <paper-input name="name" label="Name" required auto-validate></paper-input>
  <paper-button raised disabled id="eventsDemoSubmit">
</form>

<script>
  eventsDemo.addEventListener('change', function(event) {
    // Validate the entire form to see if we should enable the `Submit` button.
    eventsDemoSubmit.disabled = !eventsDemo.validate();
  });
  document.getElementById('eventsDemo').addEventListener('iron-form-submit', function(event) {
    eventsDemoSubmit.disabled = false;
  });
  ...
Share Improve this question edited May 23, 2017 at 12:34 CommunityBot 11 silver badge asked Feb 1, 2017 at 9:22 jwillmerjwillmer 3,7995 gold badges42 silver badges74 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

You could use a puted binding that takes both invalid and the value as arguments.

<paper-button disabled$="[[isDisabled(invalid, value)]]">Enable on valid input</paper-button>

The isDisabled function would then check that the value has been set. For example like this:

isDisabled: function(invalid, value) {
    return invalid || value.length === 0;
}

Here is one possible solution: Plunk

<paper-input  
                label="Insert number from the range [1, 10]"
                id="step"
                type="number" 
                min="1" 
                max="10"
                value="{{value}}"
                editable

                required

                auto-validate="true"
                invalid="{{invalid}}"
                preventInvalidInput
                error-message="value: {{value}}  - means invalid is {{invalid}}"

                on-change="stepChange">

</paper-input>


        <br>
        value: {{value}}
        <br>
        invalid: {{invalid}}
        <br><br><br>
        <paper-button raised disabled$="{{invalid}}">Enable on valid input</paper-button>

I'm not sure If I fully get your requirements. For sure you can try puted bindings and puted properties. You have a nice cheat-sheet here https://meowni.ca/posts/polymer-cheatsheet/

发布评论

评论列表(0)

  1. 暂无评论