在错误处理程序中无法访问属性

时间:2014-09-05 13:59:06

标签: dart bwu-datagrid

两个问题:

查看https://github.com/bwu-dart/bwu_datagrid/blob/master/example/src/composite_editor_item_details/app_element.dart#L120

中突出显示的行

1)为什么行

var idx = e.validationResults.errors.length;

总是会抛出错误吗?

异常:未捕获错误:null对象没有getter' length'。

NoSuchMethodError: method not found: 'length'
Receiver: null
Arguments: []
Stack Trace:
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:45)
#1      validationErrorHandler (http://localhost:8080/epimss_design.html.12.dart:184:42)
#2      _RootZone.runUnaryGuarded (dart:async/zone.dart:1020)
#3      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341)
#4      _BufferingStreamSubscription._add (dart:async/stream_impl.dart:270)
#5      _SyncBroadcastStreamController._sendData (dart:async/broadcast_stream_controller.dart:346)
#6      _BroadcastStreamController.add (dart:async/broadcast_stream_controller.dart:237)
#7      EventBus.fire (package:bwu_datagrid/core/event_bus.dart:61:19)
#8      _commitCurrentEdit (package:bwu_datagrid/bwu_datagrid.dart:3626:25)
#9      EditorLock.commitCurrentEdit (package:bwu_datagrid/core/range.dart:235:82)
#10     BwuDatagrid._commitEditAndSetFocus (package:bwu_datagrid/bwu_datagrid.dart:3045:40)
#11     _handleKeyDown (package:bwu_datagrid/bwu_datagrid.dart:2632:39)

对于其他属性也会发生同样的事情,例如字段和列等。

2)如何测试validationResult返回true?错误处理程序似乎只在存在ValidationError时触发。

我的验证器如下所示

 import 'package:bwu_datagrid/datagrid/helpers.dart' show Column, GridOptions,
    MapDataItem, MapDataItemProvider;
  import 'package:bwu_datagrid/bwu_datagrid.dart' show BwuDatagrid;
  import 'package:bwu_datagrid/formatters/formatters.dart' show CheckmarkFormatter;
  import 'package:bwu_datagrid/editors/editors.dart' show CheckboxEditor, EditorArgs,
    IntegerEditor, TextEditor;
  import 'package:bwu_datagrid/core/core.dart' show AddNewRow, ActiveCellChanged,
    ItemBase, ValidationError;
  import 'package:bwu_datagrid/plugins/row_selection_model.dart' show RowSelectionModel;

  import 'package:epimss_podo/reg.dart' show Email, EMAIL_FORM_EVENT;
  import 'package:epimss_shared/shared.dart' show toggleCoreCollapse, onBwuCellChangeHandler;
  import 'package:epimss_shared/validators.dart' show BwuRequiredEmailValidator,
    BwuRequiredNounValidator;

自定义验证器:

const String REQUIRED_EMAIL_REGEX = r"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b";
const String REQUIRED_NOUN_REGEX = r"\b[a-z'-]{2,}\b";

final RegExp _requiredNounValidator = new RegExp( REQUIRED_NOUN_REGEX, caseSensitive: false );
bool isRequiredNounValid( String property ) =>
    _requiredNounValidator.hasMatch( property );

final RegExp _requiredEmailPropertyValidator = new RegExp( REQUIRED_EMAIL_REGEX, caseSensitive: false );
bool isRequiredEmailPropertyValid( String property ) => _requiredEmailPropertyValidator.hasMatch( property );


class BwuRequiredEmailValidator extends bwu.Validator {
  bwu.ValidationResult call( dynamic value ) {
    if ( isRequiredEmailPropertyValid( value ) ) {
      return new bwu.ValidationResult( true );
    } else {
      return new bwu.ValidationResult( false, 'Valid email address required.' );
    }
  }
}


class BwuRequiredNounValidator extends bwu.Validator {
  bwu.ValidationResult call( dynamic value ) {
    if ( isRequiredNounValid( value) ) {
      return new bwu.ValidationResult( true );
    } else {
      return new bwu.ValidationResult( false, 'Valid noun is required.' );
    }
  }
}

验证错误处理程序:

void validationErrorHandler( ValidationError e ) {
  //print ( e.validationResults.errors.length );
  print ( e.column.field );

  if ( e.validationResults.isValid )
    print( 'retVal is true' );
  else
    print( 'retVal is false' );

  errorMsg = e.validationResults.message;
  var editor = e.editor;
  print ( 'valResult valid |' + e.validationResults.isValid.toString() );

  var result = e.validationResults;

  if ( e.validationResults.isValid ) {
    errorMsg = 'EMAIL';
  } else {
    errorMsg = result.message;
  }

  print( editor.runtimeType ); // aslways print TextEditor

  if ( editor != null ) {
    //var colId = editor.column.id;
    if ( editor is TypeEditor ) {
      email.isTypeValid = true;
    }

    if ( editor is AddressEditor ) {
      email.isAddressValid = false;
    }

    //print( encode ( email ) );
  }
}

1 个答案:

答案 0 :(得分:0)

您收到异常,因为字段e.validationResults.errors为空 您无法访问length的{​​{1}}属性,因此会抛出异常 字段null为空,因为在此调用中

errors

您未传递可选return new bwu.ValidationResult( false, 'Valid email address required.' ); 参数

的值
errors

提示:

据我所知,断点不适用于class ValidationResult { bool isValid = false; String message; List<ValidationErrorSource> errors; ValidationResult(this.isValid, [this.message, this.errors]); } 标签内的代码(我在电子邮件中的示例代码中看到了这一点)。
因此,我建议您将代码从<script>文件移至email_form.html文件 然后,您可以使用调试器并在运行时调查这些值,这也有助于了解其他人的代码实际正在做什么。

enter image description here

来自编辑

的消防自定义事件
email_form.dart

//您可以注册与验证错误事件相同的处理程序 grid.eventBus.onEvent(AddressEditor.VALIDATION_SUCCEEDED)。听(validationErrorHandler);

我没有尝试过这段代码,但应该可以使用。