diff --git a/src/validation-controller-factory.ts b/src/validation-controller-factory.ts index 52487ab9..5774eab6 100644 --- a/src/validation-controller-factory.ts +++ b/src/validation-controller-factory.ts @@ -1,5 +1,6 @@ import {Container} from 'aurelia-dependency-injection'; import {ValidationController} from './validation-controller'; +import {Validator} from './validator'; /** * Creates ValidationController instances. @@ -12,19 +13,21 @@ export class ValidationControllerFactory { constructor(private container: Container) {} /** - * Creates a new controller and registers it in the current element's container so that it's - * available to the validate binding behavior and renderers. + * Creates a new controller instance. */ - create() { - return this.container.invoke(ValidationController); + create(validator?: Validator) { + if (!validator) { + validator = this.container.get(Validator); + } + return new ValidationController(validator); } /** * Creates a new controller and registers it in the current element's container so that it's * available to the validate binding behavior and renderers. */ - createForCurrentScope() { - const controller = this.create(); + createForCurrentScope(validator?: Validator) { + const controller = this.create(validator); this.container.registerInstance(ValidationController, controller); return controller; } diff --git a/test/validation-controller-factory.ts b/test/validation-controller-factory.ts index 8fec0869..7b69af08 100644 --- a/test/validation-controller-factory.ts +++ b/test/validation-controller-factory.ts @@ -1,16 +1,22 @@ import {Container, Optional} from 'aurelia-dependency-injection'; import { ValidationControllerFactory, - ValidationController + ValidationController, + Validator } from '../src/aurelia-validation'; describe('ValidationControllerFactory', () => { it('createForCurrentScope', () => { const container = new Container(); + const standardValidator = {}; + container.registerInstance(Validator, standardValidator); const childContainer = container.createChild(); const factory = childContainer.get(ValidationControllerFactory); const controller = factory.createForCurrentScope(); + expect(controller.validator).toBe(standardValidator); expect(container.get(Optional.of(ValidationController))).toBe(null); expect(childContainer.get(Optional.of(ValidationController))).toBe(controller); + const customValidator = {}; + expect(factory.createForCurrentScope(customValidator).validator).toBe(customValidator); }); });