2017-08-05 81 views
1

我試圖將aurelia-dialog與aurelia-validation一起使用。事情是當我第一次打開彈出窗口並觸發驗證時,我得到一個正確的錯誤。每當我該事件後重新打開彈出窗口中,我得到了相同的驗證重複兩次,三次,等Aurelia對話框重複驗證

這是彈出

import { inject } from 'aurelia-framework'; 
import { DialogController } from 'aurelia-dialog'; 
import { ValidationController, ValidationRules } from 'aurelia-validation'; 
import { MaterializeFormValidationRenderer } from './../resources/elements/materialize-form-validation-renderer'; 

@inject(DialogController, ValidationController) 
export class AddProject { 

    rules = ValidationRules 
     .ensure('name') 
     .required() 
     .withMessage('Please enter a name for your project.') 
     .rules; 

    constructor(dialogController, validationController) { 
     this.name = ''; 
     this.project = null; 
     this.dialogController = dialogController; 
     this.validationController = validationController; 
     this.validationController.addRenderer(new MaterializeFormValidationRenderer()); 
    } 

    activate(project) { 
     this.project = project; 
    } 

    detached() { 
     this.validationController.reset(); 
    } 

    validateModel() { 
     return new Promise((resolve, reject) => { 
      this.validationController.validate().then(v => { 
       resolve(v.valid); 
      }); 
     }, function (error) { 
      reject(error); 
     }); 
    } 

    validateProject(e) { 
     this.validateModel().then(valid => { 
      if (valid) { 
       this.project.name = this.name; 
       this.name = ''; 
       //this.validationController.reset(); 
       this.dialogController.ok(this.project); 
      } 
     }); 
    } 

    cancel(e) { 
     this.name = ''; 
     //this.validationController.reset(); 
     this.dialogController.cancel(); 
    } 
} 

我打電話彈出從dialogService

視圖模型
import { inject } from 'aurelia-framework'; 
import { DialogService } from 'aurelia-dialog'; 

import { AddProject } from './add-project'; 
import { Project } from './project'; 

@inject(DialogService) 
export class ListProjects { 

    constructor(dialogService) { 
     this.dialogService = dialogService; 
    } 

    createProject() { 
     var project = new Project(); 
     this.dialogService.open({ viewModel: AddProject, model: project, lock: false }).whenClosed(response => { 
      if (!response.wasCancelled) { 
       console.log('good - ', response.output); 
      } 
     }); 
    } 

} 

任何想法如何重置驗證?我試着用這個.dialogController.cancel(),但它沒有工作。

回答

0

每次創建新的AddProject模式時,都需要使用新實例將ValidationController綁定到對話框容器上下文。

嘗試增加

@inject(DialogController, NewInstance.of(ValidationController))

,而不是僅僅

@inject(DialogController, ValidationController)

NewInstance可以aurelia-dependency-injection發現(並可能通過aurelia-framework暴露以及)

即: import {inject, NewInstance} from 'aurelia-dependency-injection';

欲瞭解更多信息,請查看Aurelia Doc Hub(http://aurelia.io/hub.html#/doc/article/aurelia/validation/latest/validation-basics/12