Skip to content

Ticket #600: Skip validation for readonly fields #283

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

Merged
merged 3 commits into from
Aug 23, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/components/mixins/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@ export default {
'validationField',
'validationMessages'
],
computed: {
isReadOnly() {
if (this.readonly || this.disabled || this.$attrs.readonly || this.$attrs.disabled) {
return true;
} else {
return false;
}
}
},
data() {
return {
validator: null
}
},
mounted() {
this.setValidatorLanguage();
this.updateValidation()
this.updateValidation();
this.observeElementMutations();
},
watch: {
// Triggered whenever the v-model is updated
Expand All @@ -29,6 +39,12 @@ export default {
label() {
this.updateValidation()
},
readonly() {
this.updateValidation();
},
disabled() {
this.updateValidation();
},
validationData: {
handler: function() {
this.updateValidation()
Expand All @@ -37,6 +53,20 @@ export default {
}
},
methods: {
observeElementMutations() {
new MutationObserver(this.handleMutations).observe(this.$el, {
attributes: true,
attributeFilter: ['readonly', 'disabled'],
subtree: true
});
},
handleMutations(mutations) {
mutations.forEach(mutation => {
if (mutation.type == "attributes") {
this.updateValidation()
}
});
},
setValidatorLanguage() {
let globalObject = typeof window === 'undefined' ? global : window;

Expand All @@ -53,7 +83,7 @@ export default {
globalObject.validatorLanguageSet = true;
},
updateValidation() {
if (this.validation) {
if (this.validation && !this.isReadOnly) {
let fieldName = this.validationField ? this.validationField : this.name;
let data = this.validationData ? this.validationData : {[fieldName]: this.value}
let validationRules = '';
Expand Down