如何在w2ui上添加自定义验证?

时间:2016-01-13 12:22:59

标签: w2ui

我有两个字段ReasonDate和RegistrationDate。根据业务逻辑,RegistrationDate不能早于ReasonDate。我正在检查'onValidate'事件处理程序,形式如下:

onValidate: function(event) {
    var pattern = /(\d{2})\.(\d{2})\.(\d{4})/;
    var reasonDt = new Date(w2ui.form.record.ReasonDate.replace(pattern, '$3-$2-$1'));
    var registrationDt = new Date(w2ui.form.record.RegistrationDate.replace(pattern, '$3-$2-$1'));

    if (reasonDt > registrationDt) {
        $('#ReasonDate').w2tag('@TranslateText("accard_ReasonDateShouldLessThanRegDate")');
        //$('#ReasonDate').addClass('w2ui-error');
    }
},

并点击按钮我正在验证如下:

add: function () {
    $('#BudgOrgTin').removeClass('w2ui-error');
    var errors = w2ui.form.validate(true);
    if (errors.length > 0) {
        return;
    }

如何正确添加我的错误消息,以便在我验证表单时它也会返回我的错误?

1 个答案:

答案 0 :(得分:1)

我在w2ui的源代码中找到了解决方案:

onValidate: function(event) {
  var pattern = /(\d{2})\.(\d{2})\.(\d{4})/;
  var reasonDt = new Date(w2ui.form.record.ReasonDate.replace(pattern, '$3-$2-$1'));
  var registrationDt = new Date(w2ui.form.record.RegistrationDate.replace(pattern, '$3-$2-$1'));

  if (reasonDt > registrationDt) {

     event.errors.push({field: this.get('RegistrationDate'), 
                        error: '@TranslateText("accard_ReasonDateShouldLessThanRegDate")'})

     // **no need for this anymore** w2ui does it for you
     // $('#ReasonDate').w2tag('@TranslateText("accard_ReasonDateShouldLessThanRegDate")');
     //$('#ReasonDate').addClass('w2ui-error');
  }
},