dart
  • polymer-1.0
  • dart-polymer
  • 2016-12-29 101 views 1 likes 
    1

    javascript類似,並執行IronValidatorBehavior如何在聚合物飛鏢中進行自定義驗證?如何在聚合物飛鏢中進行自定義驗證

    我可以創建一個新的自定義驗證器,它實現了IronValidatorBehavior

    <dom-module id="form-input"> 
        <template> 
         <style include='shared-styles iron-flex iron-flex-alignment'> 
         :host { 
          display: block; 
          padding: 1em; 
         } 
         </style> 
         <custom-validator id="validator" validator-name="jsonValidator" validator-type="json"></custom-validator> 
         <paper-textarea label="[[label]]" id="body" name="body" autofocus tabindex="0" 
             validator="jsonValidator" auto-validate error-message="This is not a valid format"></paper-textarea> 
        </template> 
        </dom-module> 
    

    的自定義驗證寫爲

    library main_app.custom_validator; 
    
    import 'dart:html'; 
    import 'package:polymer/polymer.dart'; 
    import 'package:polymer_elements/iron_validator_behavior.dart' show IronValidatorBehavior; 
    
    @PolymerRegister('custom-validator') 
    class CustomValidator extends PolymerElement with IronValidatorBehavior{ 
    
        CustomValidator.created() : super.created(); 
    
        @reflectable 
        bool validate(e, [_]){ 
        switch(validatorType) { 
         case 'json': 
         return textValidator(e); 
        } 
        return false; 
        } 
    
        bool textValidator(e){ 
        return false; 
        } 
    } 
    

    回答

    0

    沒關係,這是一個錯字,上面的作品,如果我改變。

    <custom-validator id="validator" validator-name="jsonValidator" validator-type="json"></custom-validator> 
    

    <custom-validator id="validator" validator-name="jsonValidator" validator-check="json"></custom-validator> 
    

    酒店validatorType需要將其設置爲 '驗證' 的默認值。相反,添加了一個新的屬性validatorCheck,我在其中設置要調用的驗證方法名稱。

    library custom_validator; 
    
    import 'dart:mirrors' show reflect;  
    import 'package:polymer/polymer.dart'; 
    import 'package:polymer_elements/iron_validator_behavior.dart' show IronValidatorBehavior; 
    import 'package:validator/validator.dart' show isJSON; 
    
    @PolymerRegister('custom-validator') 
    class CustomValidator extends PolymerElement with IronValidatorBehavior{ 
        @property 
        String validatorCheck; 
    
        CustomValidator.created() : super.created(); 
    
        @reflectable 
        bool validate(value, [_]){ 
    
        if(validatorCheck.isNotEmpty) { 
         Symbol sym = new Symbol(validatorCheck); 
         return reflect(this).invoke(sym, [value]).reflectee; 
        } 
        return false; 
        } 
    
        bool json(String value, [_]){ 
    
        if(value.isNotEmpty) { 
         return isJSON(value); 
        } 
        return true; 
        } 
    } 
    
    相關問題