Skip to content

Commit 5659756

Browse files
committed
Allow injecting validate.js options
It's now possible to set up a static `validatorOptions` object in the stimulus controller. This object will be used to pass any extra options accepted by validate.js. Eg, validate.js has an option to skip prepending the error msgs with the attr name: `fullMessages`. When setting this option to false, instead of an error msg being "Foo can't be blank", it'll be "can't be blank". It's now easy to pass this option by setting: static rules = {...} static validatorOptions = { fullMessages: false } ---
1 parent f50772a commit 5659756

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/validator.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,21 @@ export class Validator {
5353
}
5454

5555
const { value } = this.attributes.get(attribute)
56-
return [{ [attribute]: value }, { [attribute]: this.rules[attribute] }]
56+
return [
57+
{ [attribute]: value },
58+
{ [attribute]: this.rules[attribute] },
59+
this.validatorOptions || {}
60+
]
5761
}
5862

5963
get rules() {
6064
return this.controller.constructor.rules
6165
}
6266

67+
get validatorOptions() {
68+
return this.controller.constructor.validatorOptions
69+
}
70+
6371
get errors() {
6472
return this.controller.errors
6573
}

0 commit comments

Comments
 (0)