2017-01-12 11 views
0

我想將單個aurelia驗證匹配到表單中的一組屬性。例如。映射到表中的行的對象數組中的屬性名稱爲ssid的每個字段。驗證文檔說,確保子句可以有一個property expression這聽起來像我所需要的。我使用的是從aurelia驗證屬性表達式幫助需要

{ValidationRules, ValidationController} from 'aurelia-validation';

驗證我使用

ValidationRules.ensure('apPwd').displayName('XY AP Password').maxLength(32).minLength(8).on(this);

其中apPwd是屬性表達式得到驗證一個單一屬性的工作。

但我找不到屬性表達式的任何規範。 aurelia文檔中的大多數示例都只顯示一個屬性名稱。我見過的最複雜的東西是與|&(不管是什麼)配合在一起。

任何人都可以指向我的規範或幫助解決我的具體問題嗎?或者,也許我應該拋棄這個軟件包並推出自己的代碼?

回答

0

我認爲這可以幫助你:

這是validate.ts

import { autoinject } from 'aurelia-framework'; 
import { ValidationRules, ValidationController } from 'aurelia-validation'; 

@autoinject 
export class Validate { 
    numberField: number; 

    controller: any; 

    constructor (
     private validationController: ValidationController 
    ) { 
     this.controller = validationController; 

     ValidationRules.customRule(
      'nummer', 
       (value, obj, min, max) => { 
        let numberValue: number = parseInt(value); 
        return value === null || value === undefined || Number.isInteger(nummer) && nummer >= min && nummer <= max; 
       }, 
       `\${$displayName} must be an integer between \${$config.min} and \${$config.max}.`, 
       (min, max) => ({ min, max }) 
      ); 

     ValidationRules 
      .ensure((m: this) => m.numberField) 
       .required() 
       .withMessage('Rutan får inte vara tom.') 
       .satisfiesRule('nummer', 1, 100) 
       /*.satisfies((value: any, object?: any) => { 
        console.log(value); 
        if (value >= 1 && value <= 100) { 
         return true; 
        } 
        else { 
         return false; 
        } 
       }) 
       .withMessage(`The number must be between - 100.`)*/ 
      .ensure((m: this) => m.numberField).required() 
      .on(this); 
    } 

    submit(): void { 
     this.executeValidation(); 
    } 

    executeValidation(): void { 
     this.controller.validate() 
     .then(errors => { 
      if (errors.length === 0) { 
       console.log('all good!'); 
      } else { 
       console.log('all errors!'); 
      } 
     }); 
    } 

} 

,這是validate.pug(在我的情況,但如果你喜歡,你可以使用HTML頁面)如果

template 
    section.au-animate 
     h2 Validate 

     form(role="form") 
      .form-group 
       label Number 
       input#meetingSubject.form-control(type="text" value.bind="numberField & validate") 

      button.btn.btn-lg.btn-primary(click.delegate='submit()') Validate 
     div 
      h3 ${'latestValidationResult'} 
      ul(if.bind='controller.errors.length') 
       li(repeat.for='error of controller.errors') ${error.message} 
+0

令人驚訝的是,我想我明白你所做的一切,除了'.ensure(m => m.numberField)''。這個函數是aurelia文檔稱爲「屬性表達式」的嗎?這個功能的意義在於我。它何時被調用,爲什麼?並且一個指向文檔的指針會很棒。 –

0

不知道我理解你的問題,但我認爲你正在尋找ensureObject

ValidationRules 
    .ensureObject() 
    .satisfies(obj => { 
     return obj.property1 === 'test' && obj.property2.indexOf('test2') !== -1 
    }) 
    .withMessage('Some error message.') 
    .on(this); 
+0

我在aurelia文檔中搜索了'ensureObject',結果什麼都沒發現。你能告訴哪裏可以找到它嗎?也許你可以解釋一下'obj'是指什麼。如何獲得'obj'來測試數組中的每個對象,比如'this.rows = [{prop:1},{prop:2}]''中的每個'prop'屬性? –

+0

你可以在http://aurelia.io/hub.html#/doc/article/aurelia/validation/latest/validation-basics/8找到文檔。如果要驗證數組中的每個屬性,只需遍歷數組並返回false,如果其中一個值不符合條件 –

+0

'ensureObject'不在該文檔鏈接的搜索結果中。此外,無論我改變「ValidationRules.ensureObject()。satisfies(obj => console.log(obj))。on(this);''的形式,該代碼都不會產生任何輸出。我想我會繼續黑客攻擊它。 –