如何在Polymer Dart中进行自定义验证

时间:2016-12-29 05:51:08

标签: dart polymer-1.0 dart-polymer

javascript// This method will be called when app received push notifications in foreground func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { completionHandler(UNNotificationPresentationOptions.alert) } 的实现类似,如何在Polymer Dart中进行自定义验证?

我可以创建一个新的自定义验证器来实现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>

1 个答案:

答案 0 :(得分: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需要设置为默认值&#39;验证器&#39;。而是添加了一个新属性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;
  }
}