How to enforce required paper-radio-group in Polymer?

前端 未结 1 392
走了就别回头了
走了就别回头了 2021-01-29 01:09

What\'s the best way to enforce require check on paper-radio-group? I saw another question and answer that uses fallback-selection, here,

1条回答
  •  孤街浪徒
    2021-01-29 01:24

    I'm assuming you call checkAnswers() in your submit handler. Note that .validate() performs similar logic except it only calls validate() on children that have the IronFormElementBehavior and the required attribute. If you apply required to the appropriate input elements, you could replace checkAnswers() with this.$.form.validate().

    submit: function() {
      //var isValid = checkAnswers();
      var isValid = this.$.form.validate();
    }
    

    actually does not have the IronFormElementBehavior, so its required attribute has no effect. You could workaround this by wrapping with your own custom element that properly adds the behavior:

    
      
      
    
    

    Then, just replace with :

    
      Yes
      No
    
    

    
      
      
      
      
      
      
      
      
    
    
    
    
    
      
      
    
    
    
      
      
    
    

    codepen

    For binary input like these Yes/No answers, it might be appropriate to use instead, as it requires less code and simplifies the form for that input.

    
      
      
      
      
      
      
    
    
    
      
    
      
        
        
      
    

    codepen

    0 讨论(0)
提交回复
热议问题