2017-04-26 6 views
2

HTMLでレンダリングする際にエラーが発生する問題が発生しています。入力ごとの検証エラーを表示するAurelia

のViewModel:

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

// Models 
import { NewCustomer } from '../../models/customer'; 

@autoinject 
export class Register { 
    controller: ValidationController; 
    customer: NewCustomer; 

    constructor(controllerFactory: ValidationControllerFactory, customer: NewCustomer) { 
     this.controller = controllerFactory.createForCurrentScope(); 
     this.customer = customer; 
     this.controller.addObject(this.customer); 
    } 

    validate(): void { 
     this.controller.validate().then(res => { 
      console.log(res); 
      if(res.valid) { 
       this.register(); 
      } 
     }); 
    } 
} 

ValidationRules 
    .ensure((c: NewCustomer) => c.first_name).displayName('first name').required().withMessage(`\${$displayName} cannot be blank.`) 
    .ensure((c: NewCustomer) => c.last_name).displayName('last name').required().withMessage(`\${$displayName} cannot be blank.`) 
    .ensure((c: NewCustomer) => c.email).displayName('first name').email().required().withMessage(`\${$displayName} cannot be blank.`) 
    .ensure((c: NewCustomer) => c.phone_number).displayName('phone number').matches(/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/).withMessageKey('invalid phone number').required().withMessage(`\${$displayName} cannot be blank.`) 
    .ensure((c: NewCustomer) => c.password).displayName('password').minLength(7).required().withMessage(`\${$displayName} cannot be blank.`) 
    .on(NewCustomer); 

とし、例えば、入力:

<div class="sb-input-group no-margin" validation-errors.bind="first_nameErrors"> 
     <label>First Name</label> 
     <input value.bind="customer.first_name" placeholder="First Name" class="sb-input-control"> 
     <span class="help-block danger" repeat.for="errorInfo of first_nameErrors"> 
      ${errorInfo.error.message} 
     <span> 
    </div> 

今、私はフォームを送信し、私は検証ルールが正しくピックアップされていることがわかりコンソールをチェックしたときに、ビューにレンダリングされません。私もvalidation-errors.bind="customer.first_nameErrors"をやってみましたが、うまくいきませんでした。エラーをビューにバインドする正しい形式は何ですか?

編集:ここでは、あなたの検証コントローラとの結合のインスタンスを登録するには、HTMLで「&検証」マークアップが欠落しているように見えますNewCustomerオブジェクト

export class NewCustomer { 
    first_name: string; 
    last_name: string; 
    email: string; 
    phone_number: string; 
    password: string; 
    companies: Company[]; 
    selected_company_id: string; 
} 

答えて

2

です。

また、バリデーションレンダラー(おおよそBootstrap Validation Renderersがあります)をチェックアウトすると、どこからでもエラースパンを追加することができます。

これはほとんどがthe docsにありますが、すべてを理解するために多くの手間がかかりました。

関連する問題