Skip to content

Commit 78c544b

Browse files
committed
breaking(pat-validation): Remove error-template option.
This is a breaking change. Due to a Content-Security-Policy problem with dom.template when unsafe-eval is not set - which you wouldn't set if possible - we had to remove the error-template parameter. Instead the template is now defined in a error_template method on the Patten class and can be customized by subclassing and extending the pat-validation pattern or by patching it via Pattern.prototype.
1 parent 989fa9f commit 78c544b

File tree

4 files changed

+22
-20
lines changed

4 files changed

+22
-20
lines changed

src/pat/validation/documentation.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,5 +79,3 @@ Error messages can also be overridden on a per-field basis, for example:
7979
| message-required | The error message for required fields. | This field is required. | String |
8080
| not-after | Field-specific. A lower time limit restriction for date and datetime fields. | | CSS Selector or a ISO8601 date string. |
8181
| not-before | Field-specific. An upper time limit restriction for date and datetime fields. | | CSS Selector or a ISO8601 date string. |
82-
| error-template | The template to be used to render the error message. | <em class="validation warning message">${this.message}</em> | String |
83-

src/pat/validation/validation.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,10 @@ parser.addArgument("not-before", null);
2626
parser.addArgument("equality", null);
2727
parser.addArgument("delay", 100); // Delay before validation is done to avoid validating while typing.
2828

29-
// Template for the validation message
30-
parser.addArgument(
31-
"error-template",
32-
'<em class="validation warning message">${this.message}</em>'
33-
);
34-
3529
// BBB
3630
// TODO: deprecated. Will be removed with next major version.
3731
parser.addAlias("message-integer", "message-number");
32+
parser.addArgument("error-template");
3833

3934
const KEY_ERROR_EL = "__patternslib__input__error__el";
4035
const KEY_ERROR_MSG = "__patternslib__input__error__msg";
@@ -324,7 +319,7 @@ export default Base.extend({
324319
event.stopPropagation();
325320
event.stopImmediatePropagation();
326321
}
327-
this.set_error_message(input, input_options);
322+
this.set_error_message(input);
328323
},
329324

330325
set_validity({ input, msg, attribute = null, min = null, max = null }) {
@@ -372,7 +367,7 @@ export default Base.extend({
372367
}
373368
},
374369

375-
set_error_message(input, options) {
370+
set_error_message(input) {
376371
this.remove_error(input);
377372

378373
// Do not set a error message for a input group like radio buttons or
@@ -385,10 +380,9 @@ export default Base.extend({
385380

386381
// Create the validation error DOM node from the template
387382
const validation_message = input.validationMessage || input[KEY_ERROR_MSG];
388-
const error_template = dom.template(options.errorTemplate, {
389-
message: validation_message,
390-
});
391-
const error_node = dom.create_from_string(error_template).firstChild;
383+
const error_node = dom.create_from_string(
384+
this.error_template(validation_message)
385+
).firstChild;
392386

393387
let fieldset;
394388
if (input.type === "radio" || input.type === "checkbox") {
@@ -423,4 +417,9 @@ export default Base.extend({
423417
}
424418
}
425419
},
420+
421+
error_template(message) {
422+
// Template for the validation message
423+
return `<em class="validation warning message">${message}</em>`;
424+
},
426425
});

src/pat/validation/validation.test.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,20 +215,23 @@ describe("pat-validation", function () {
215215
expect(el.querySelector("#form-buttons-create").disabled).toBe(false);
216216
});
217217

218-
it("1.9 - can define a custom error message template.", async function () {
219-
const _msg = "${this.message}"; // need to define the template in normal quotes here to not make the parser expand the missing variable.
218+
it("1.9 - can define a custom error message template by subclassing.", async function () {
219+
class CustomValidation extends Pattern {
220+
error_template(message) {
221+
return `<div class="validation-error">${message}</div>`;
222+
}
223+
}
224+
220225
document.body.innerHTML = `
221226
<form class="pat-validation"
222-
data-pat-validation='
223-
message-required: need this;
224-
error-template: &lt;div class="validation-error"&gt;${_msg}&lt;/div&gt;'>
227+
data-pat-validation="message-required: need this;">
225228
<input type="text" name="name" required>
226229
</form>
227230
`;
228231
const el = document.querySelector(".pat-validation");
229232
const inp = el.querySelector("[name=name]");
230233

231-
new Pattern(el);
234+
new CustomValidation(el);
232235
await utils.timeout(1); // wait a tick for async to settle.
233236

234237
inp.value = "";

webpack/webpack.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ module.exports = () => {
6060

6161
if (process.env.NODE_ENV === "development") {
6262
config.devServer.static.directory = path.resolve(__dirname, "../");
63+
config.devServer.headers["Content-Security-Policy"] =
64+
"default-src https: http: data: 'self' 'unsafe-inline'; script-src https: http: data: 'self' 'unsafe-inline';";
6365
}
6466

6567
// Add an @patternslib/patternslib alias when building within this repository.

0 commit comments

Comments
 (0)