4

我正在使用PaperInput并且喜欢这种感觉。但是,有没有办法使用我自己的逻辑进行验证?例如,在某些情况下,模式匹配不足以确定我想要显示的错误。一个例子是我希望PaperInput指定一个只能添加一次的项目,因此验证将在某些模型映射中进行查找,如果input.inputValue不存在,则它是有效的,否则无效。

  <paper-input floatingLabel
               id="alias-input"
               validate="{{aliasIsValid}}"
               type="text"
               error="{{aliasError}}"
               label="Person Alias (eg: King, Eldest Son, Mooch, etc.)"
               required
               ></paper-input>

所以,我希望能够实现bool aliasIsValid()并在验证无效时设置@observable String aliasError 。我不认为这是它的工作原理,但有没有办法实现这一点?

4

1 回答 1

2

聚合物.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', [""]);
    }
  }
}

仅在输入元素失去焦点时验证validateImmediately从 HTML 元素中删除并使用on-change事件代替(未测试)。

<paper-input id="custom" on-input="{{inputHandler}}" validateImmediately></paper-input>

我在https://github.com/dart-lang/core-elements/pull/102添加了一条评论,以便在下次更新时直接在 Dart 中使用此方法。

<core-input>声明支持 HTML5 约束验证 API的文档。有关更多信息,请参阅 https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/HTML5/Constraint_validation

于 2014-09-09T18:09:38.753 回答