adopted-ember-addons / adopted-ember-addons/ember-cp-validations

Disabled check not recomputing immediately on some attributes

Open
#596 12 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
439
Forks
172
PR merge metrics
No merged PRs in 30d

Description

### Environment

- Ember Version: 3.0.0
- Ember CLI Version: 3.0.0
- Ember CP Validations Version: 3.5.1

### Steps to Reproduce

I noticed that for my custom validator, it wasn't quite working correctly, the disabled check was being cached or something - let me explain.

Validator
```javascript
// Use this validator to determine if datetime in model is before given compare date
const IsBeforeCheck = BaseValidator.extend({
validate(value, options, model, attribute) {
/* Some custom validation code here */
return isValid ? isValid : this.createErrorMessage('error message etc.', value, options);
}
});
IsBeforeCheck.reopenClass({
getDependentsFor(attribute, options) {
return [];
}
});
export default IsBeforeCheck;
```

Model
```javascript
'scheduledTime': {
validators: [
validator('isBefore', {
compareDate: computed('model.reportParams.endDate',(edt) => edt),
disabled: computed('model.reportType','model.reportParams.isDateRange', function() {
let reportType = this.get('model.reportType');
let isDateRange = this.get('model.reportParams.isDateRange');

let result = false;
if(reportType === 'tpms_current_status') result = result || true;
if(!isDateRange) result = result || true;
console.log(result);
return result;
}),
dependentKeys: [
'model.reportType',
'model.reportParams.isDateRange',
'model.reportParams.endDate'
],
})
]
},
```
NOTE: I also have a global debounce value set to 500 - but I tried setting this to 0 and it didn't fix anything.

With the code above, I noticed that if I changed the `isDateRange` flag, or modified the `endDate`, the validator would fire correctly and disabled would be correctly set. But if I changed the `reportType`, the validator was doing strange things.

The only way the `reportType` is set is via the template on button click, eg `{{action (mut model.reportType) 'tpms_current_status'}}`

The validator would correctly detect that the `reportType` changed and that re-validation needed to happen, but it didn't necessarily have the correct option values. Basically, when disabled should have returned true, it would return false, and vise versa.

I narrowed it down to this;
* If I set reportType to tpms_current_status so that disabled should return TRUE, at this point in the code, disabled incorrectly returns false and continues with the validation (it doesn't even try to call the disabled CP, just gets a cached value)
https://github.com/offirgolan/ember-cp-validations/blob/d4e720dbb2fef38b996a45357a446b67a4e672ba/addon/validations/factory.js#L503-L509
* But by this point in the code, disabled does have the correct value - but there is no disabled check here, so it continues with the validation anyway
https://github.com/offirgolan/ember-cp-validations/blob/d4e720dbb2fef38b996a45357a446b67a4e672ba/addon/validations/factory.js#L522

Anyone know what's going on here? Am I doing something wrong? It works most other times I've used it, I can't figure out why this is any different

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.