aurelia / validation

A validation plugin for Aurelia.
MIT License
132 stars 128 forks source link

Aurelia validation not working for derived classes #148

Closed laurentiustamate94 closed 8 years ago

laurentiustamate94 commented 9 years ago

I detalied the problem here. I was working with validation@0.2.8 and when I updated to validation@0.3.1 this problem appeared.

From what I have investigated, there is a problem on how reflection metadata works and how it is added to the current object.

jods4 commented 9 years ago

You need to provide more info, even considering the stack overflow post.

Which browser are you running on? Something that supports classes / something that supports setting a prototype / none of that (e.g. IE up to 10)?

I assume you use TS to transpile to ES5? Or maybe TS to ES6 then to Babel for ES5? Or you just use ES6?

Now if the issue is about modern browsers, each class gets its own metadata store, look at how you had to @inject(validation) on DerivedClass although BaseClass already did.

It would be convenient if aurelia-validation would walk up the inheritance chain and merge the validation metadata along the way... Although I feel this should be handled in a more generalized way by aurelia-metadata or even Reflect rather than specifically for aurelia-validation.

I think that walking the inheritance chain for metadata is probably not going to work well on IE9-10 (which are on Aurelia support list).

At the time being, you're better off using the API to set up the validation rather than metadata in a class hierarchy. Something like this would work:

class BaseClass {
  validation: ValidationGroup;

  constructor(validation: Validation) {
    this.validation = validation.on(this);
    this.setupValidation();
  }

  protected setupValidation() {
    this.validation.ensure('firstName').isNotEmpty();
  }
}

class DerivedClass extends BaseClass {
  protected setupValidation() {
    super.setupValidation();
    this.validation.ensure('lastName').isNotEmpty();
  }
}
plwalters commented 8 years ago

Thanks for submitting this / commenting on this. At this time we are closing this because we have completely re-written and are deprecating the previous feature set. If you feel this should be re-opened please feel free to review this blog post and submit either again on this repository, or on the new validatejs bridge repository

Thanks again!