Skip to content

Commit 04ce1b0

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 04ce1b0

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

spec/validator_spec.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ describe("Validator", function() {
2525
rules: {
2626
name: { presence: { allowEmpty: false } },
2727
email: { presence: { allowEmpty: false }, email: true }
28+
},
29+
validatorOptions: {
30+
fullMessages: false
2831
}
2932
}
3033
}
@@ -127,7 +130,8 @@ describe("Validator", function() {
127130
it("returns validatajs params", function() {
128131
const params = [
129132
{ name: "" },
130-
{ name: { presence: { allowEmpty: false } } }
133+
{ name: { presence: { allowEmpty: false } } },
134+
{ fullMessages: false }
131135
]
132136

133137
expect(this.validator.validatejsParams("name")).to.eql(params)

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)