Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I'm submitting a bug report - Conditional Validation doesn't update when property changes. #392

Closed
ClareBear85 opened this issue Dec 1, 2016 · 2 comments
Labels

Comments

@ClareBear85
Copy link

ClareBear85 commented Dec 1, 2016

Pre - Validation Update

this.validator = this.validation.on(this)
                    .ensure('baseContent.SettingId', (config) => {config.computedFrom(['IsDiscontinued'])})
                    .if(() => { return !this.IsDiscontinued })
                        .isNotEmpty()
                        .isGreaterThan(0).withMessage('is required')     
                    .endIf()

Post - Validation Update

ValidationRules.ensure('SettingId').required().when(v => v.IsDiscontinued);

@computedFrom('isDiscontinued')
    get IsDiscontinued()
    {
        this.readonly = this.isDiscontinued;
        return this.isDiscontinued;
    }

If the value of IsDiscontinued changes the validation rule is not affected and will stay either on or off depending on what it was on first loaded.

Is there a way to allow the conditional rule to be applied on the fly?

@jdanyow
Copy link
Contributor

jdanyow commented Dec 3, 2016

@ClareBear85 we're tracking this in #279

As a workaround you could do something like this:

@computedFrom('isDiscontinued')
get IsDiscontinued() {
  return this.isDiscontinued;
}
set IsDiscontinued(newValue) {
  if (newValue === this.isDiscontinued) {
    return;
  }
  this.controller.validate({ object: this, propertyName: `SettingId` });
  this.isDiscontinued = newValue;
}

(then make sure you never use isDiscontinued directly and instead use the IsDiscontinued property).

@ClareBear85
Copy link
Author

ClareBear85 commented Dec 5, 2016

@jdanyow -

Where do I actually put

.isNotEmpty()
.isGreaterThan(0).withMessage('is required')    

And if there is more than one property. Do I do this:

this.controller.validate({ object: this, propertyName: `SettingId` });
this.controller.validate({ object: this, propertyName: `SettingTwoId` });

or 

this.controller.validate({ object: this, propertyName: [`SettingId`, `SettingTwoId`] });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants