Taking total control of PaperInput validation

前端 未结 1 381
不思量自难忘°
不思量自难忘° 2020-12-11 08:21

I\'m using PaperInput and like the feel. But, is there a way to do the validation using my own logic? For instance, in some cases a pattern match is not enough to d

1条回答
  •  有刺的猬
    2020-12-11 08:52

    Polymer.dart <= 0.16.x

    import 'dart:html';
    import 'package:polymer/polymer.dart';
    import 'package:core_elements/core_input.dart';
    
    @CustomTag('app-element')
    
    class AppElement extends PolymerElement {
      AppElement.created() : super.created() {}
    
      void inputHandler(Event e) {
        var inp = ($['custom'] as CoreInput);
        // very simple check - you can check what you want of courxe
        if(inp.inputValue.length < 5) {
          // any text is treated as validation error
          inp.jsElement.callMethod('setCustomValidity', ["Give me more!"]);
        } else {
          // empty message text is interpreted as valid input
          inp.jsElement.callMethod('setCustomValidity', [""]);
        }
      }
    }
    

    To validate only when the input element loses focus remove validateImmediately from the HTML element and use the on-change event instead (not tested).

    
    

    I added a comment at https://github.com/dart-lang/core-elements/pull/102 to make this method available directly in Dart with the next update.

    The documentation of states that the HTML5 constraint validation API is supported. For more information see https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation

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