aurelia / validatejs

Enables expressive validation using decorators and/or a fluent API.
MIT License
22 stars 23 forks source link

Error navigating back away and back to a view with validation #40

Closed apawsey closed 8 years ago

apawsey commented 8 years ago

I have a fairly simple registration page with validation.

View model as follows:

import {inject} from 'aurelia-framework';
import {AuthService} from 'aurelia-auth';
import {Validator, ValidationEngine} from 'aurelia-validatejs';

@inject(AuthService, Validator)
export class Register {

    constructor(auth, validator) {
        this.validationErrors = [];
        this.email = "";
        this.password = "";
        this.confirmPassword = "";
        this.firstName = "";
        this.lastName = "";
        this.isBusy = false;
        this.auth = auth;
        this.validator = validator
            .ensure(this, 'firstName')
                .required()
                .length({ maximum: 30 })
            .ensure(this, 'lastName')
                .required()
                .length({ maximum: 30 })
            .ensure(this, 'email')
                    .required()
                .email()
            .ensure(this, 'password')
                    .required()
            .ensure(this, 'confirmPassword')
                    .required()
                .equality('password');
        this.reporter = ValidationEngine.getValidationReporter(this);
        this.observer = this.reporter.subscribe(result => {
            this.validationErrors = result;
        });
    }

    signup() {
        this.validator.validate();
        if (this.validationErrors.length === 0) {
            this.isBusy = true;
            return this.auth.signup({
                    firstName: this.firstName,
                    lastName: this.lastName,
                    email: this.email,
                    password: this.password
                })
                .then((response) => {
                    console.log("signed up");
                    this.isBusy = false;
                })
                .catch(err => {
                    this.isBusy = false;
                });
        }

    }

    detached() {
        this.observer.dispose();
    }
}

If I go to this page, do nothing except navigate away to a new page, and then try and navigate back to this view, I get the following error: (without validation, the view works fine)

ERROR [app-router] Error: Error invoking Register. Check the inner error for details.
------------------------------------------------
inner error: Error: Validator cannot handle multiple objects
    at Validator.ensure (webpack:///./~/aurelia-validatejs/dist/commonjs/validator.js?:60:13)
    at new Register (webpack:///./src/services/auth/register.js?:56:30)
    at Object.invoke (webpack:///./~/aurelia-dependency-injection/dist/commonjs/aurelia-dependency-injection.js?:334:12)
    at InvocationHandler.invoke (webpack:///./~/aurelia-dependency-injection/dist/commonjs/aurelia-dependency-injection.js?:299:166)
    at Container.invoke (webpack:///./~/aurelia-dependency-injection/dist/commonjs/aurelia-dependency-injection.js?:563:23)
    at StrategyResolver.get (webpack:///./~/aurelia-dependency-injection/dist/commonjs/aurelia-dependency-injection.js?:126:35)
    at Container.get (webpack:///./~/aurelia-dependency-injection/dist/commonjs/aurelia-dependency-injection.js?:500:21)
    at eval (webpack:///./~/aurelia-templating/dist/commonjs/aurelia-templating.js?:3831:71)
    at tryCatcher (webpack:///./~/bluebird/js/browser/bluebird.js?:5098:23)
    at Promise._settlePromiseFromHandler (webpack:///./~/bluebird/js/browser/bluebird.js?:3129:31)
    at Promise._settlePromise (webpack:///./~/bluebird/js/browser/bluebird.js?:3186:18)
    at Promise._settlePromise0 (webpack:///./~/bluebird/js/browser/bluebird.js?:3231:10)
    at Promise._settlePromises (webpack:///./~/bluebird/js/browser/bluebird.js?:3310:18)
    at eval (webpack:///./~/bluebird/js/browser/bluebird.js?:175:25)
    at MutationObserver.eval (webpack:///./~/bluebird/js/browser/bluebird.js?:4365:17)
    at Validator.ensure (webpack:///./~/aurelia-validatejs/dist/commonjs/validator.js?:60:13)
    at new Register (webpack:///./src/services/auth/register.js?:56:30)
    at Object.invoke (webpack:///./~/aurelia-dependency-injection/dist/commonjs/aurelia-dependency-injection.js?:334:12)
    at InvocationHandler.invoke (webpack:///./~/aurelia-dependency-injection/dist/commonjs/aurelia-dependency-injection.js?:299:166)
    at Container.invoke (webpack:///./~/aurelia-dependency-injection/dist/commonjs/aurelia-dependency-injection.js?:563:23)
    at StrategyResolver.get (webpack:///./~/aurelia-dependency-injection/dist/commonjs/aurelia-dependency-injection.js?:126:35)
    at Container.get (webpack:///./~/aurelia-dependency-injection/dist/commonjs/aurelia-dependency-injection.js?:500:21)
    at eval (webpack:///./~/aurelia-templating/dist/commonjs/aurelia-templating.js?:3831:71)
    at tryCatcher (webpack:///./~/bluebird/js/browser/bluebird.js?:5098:23)
    at Promise._settlePromiseFromHandler (webpack:///./~/bluebird/js/browser/bluebird.js?:3129:31)
    at Promise._settlePromise (webpack:///./~/bluebird/js/browser/bluebird.js?:3186:18)
    at Promise._settlePromise0 (webpack:///./~/bluebird/js/browser/bluebird.js?:3231:10)
    at Promise._settlePromises (webpack:///./~/bluebird/js/browser/bluebird.js?:3310:18)
    at eval (webpack:///./~/bluebird/js/browser/bluebird.js?:175:25)
    at MutationObserver.eval (webpack:///./~/bluebird/js/browser/bluebird.js?:4365:17)
    at Validator.ensure (webpack:///./~/aurelia-validatejs/dist/commonjs/validator.js?:60:13)
    at new Register (webpack:///./src/services/auth/register.js?:56:30)
    at Object.invoke (webpack:///./~/aurelia-dependency-injection/dist/commonjs/aurelia-dependency-injection.js?:334:12)
    at InvocationHandler.invoke (webpack:///./~/aurelia-dependency-injection/dist/commonjs/aurelia-dependency-injection.js?:299:166)
    at Container.invoke (webpack:///./~/aurelia-dependency-injection/dist/commonjs/aurelia-dependency-injection.js?:563:23)
    at StrategyResolver.get (webpack:///./~/aurelia-dependency-injection/dist/commonjs/aurelia-dependency-injection.js?:126:35)
    at Container.get (webpack:///./~/aurelia-dependency-injection/dist/commonjs/aurelia-dependency-injection.js?:500:21)
    at eval (webpack:///./~/aurelia-templating/dist/commonjs/aurelia-templating.js?:3831:71)
    at new AggregateError (webpack:///./~/aurelia-pal/dist/commonjs/aurelia-pal.js?:19:11)
    at Container.invoke (webpack:///./~/aurelia-dependency-injection/dist/commonjs/aurelia-dependency-injection.js?:565:13)
    at StrategyResolver.get (webpack:///./~/aurelia-dependency-injection/dist/commonjs/aurelia-dependency-injection.js?:126:35)
    at Container.get (webpack:///./~/aurelia-dependency-injection/dist/commonjs/aurelia-dependency-injection.js?:500:21)
    at eval (webpack:///./~/aurelia-templating/dist/commonjs/aurelia-templating.js?:3831:71)
From previous event:
    at CompositionEngine.ensureViewModel (webpack:///./~/aurelia-templating/dist/commonjs/aurelia-templating.js?:3824:73)
    at TemplatingRouteLoader.loadRoute (webpack:///./~/aurelia-templating-router/dist/commonjs/route-loader.js?:57:35)
    at loadComponent (webpack:///./~/aurelia-router/dist/commonjs/aurelia-router.js?:1360:22)
    at loadRoute (webpack:///./~/aurelia-router/dist/commonjs/aurelia-router.js?:1335:10)
    at eval (webpack:///./~/aurelia-router/dist/commonjs/aurelia-router.js?:1299:12)
    at Array.map (native)
    at loadNewRoute (webpack:///./~/aurelia-router/dist/commonjs/aurelia-router.js?:1298:29)
    at LoadRouteStep.run (webpack:///./~/aurelia-router/dist/commonjs/aurelia-router.js?:1290:12)
    at next (webpack:///./~/aurelia-router/dist/commonjs/aurelia-router.js?:127:18)
    at iterate (webpack:///./~/aurelia-router/dist/commonjs/aurelia-router.js?:1114:12)
    at processDeactivatable (webpack:///./~/aurelia-router/dist/commonjs/aurelia-router.js?:1117:10)
    at CanDeactivatePreviousStep.run (webpack:///./~/aurelia-router/dist/commonjs/aurelia-router.js?:1049:12)
    at next (webpack:///./~/aurelia-router/dist/commonjs/aurelia-router.js?:127:18)
    at eval (webpack:///./~/aurelia-router/dist/commonjs/aurelia-router.js?:560:14)
From previous event:
    at BuildNavigationPlanStep.run (webpack:///./~/aurelia-router/dist/commonjs/aurelia-router.js?:558:56)
    at next (webpack:///./~/aurelia-router/dist/commonjs/aurelia-router.js?:127:18)
    at Pipeline.run (webpack:///./~/aurelia-router/dist/commonjs/aurelia-router.js?:140:12)
    at eval (webpack:///./~/aurelia-router/dist/commonjs/aurelia-router.js?:1587:23)
From previous event:
    at AppRouter._dequeueInstruction (webpack:///./~/aurelia-router/dist/commonjs/aurelia-router.js?:1560:30)
    at eval (webpack:///./~/aurelia-router/dist/commonjs/aurelia-router.js?:1551:15)
From previous event:
    at AppRouter._queueInstruction (webpack:///./~/aurelia-router/dist/commonjs/aurelia-router.js?:1548:12)
    at eval (webpack:///./~/aurelia-router/dist/commonjs/aurelia-router.js?:1482:21)
From previous event:
    at AppRouter.loadUrl (webpack:///./~/aurelia-router/dist/commonjs/aurelia-router.js?:1481:51)
    at BrowserHistory._loadUrl (webpack:///./~/aurelia-history-browser/dist/commonjs/aurelia-history-browser.js?:279:53)
    at BrowserHistory._checkUrl (webpack:///./~/aurelia-history-browser/dist/commonjs/aurelia-history-browser.js?:272:12)

Apologies if any spacing is messed up in my cut and paste

EisenbergEffect commented 8 years ago

@PWKad This is because validator is a singleton. I don't think it should be. Is there any real value in injecting it at all? I'm thinking we should just have the docs show newing them up.

plwalters commented 8 years ago

We are moving it to being created from a static method on the Validation engine. Should be in Tuesday release

apawsey commented 8 years ago

Apologies, just realised this is a duplicate of issue #35. I mis-read and thought that issue was concerned with mixing fluent and decorator configuration.