[ADD] web_widget_pattern

This commit is contained in:
Holger Brunn
2024-04-23 17:17:04 +02:00
parent 5f6aa6c566
commit 7a59f5ef4d
33 changed files with 1373 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
/** @odoo-module **/
import {AutoComplete} from "@web/core/autocomplete/autocomplete";
const _extractProps = AutoComplete.extractProps;
AutoComplete.extractProps = ({attrs, field}) => {
return Object.assign(_extractProps({attrs, field}), {
pattern: attrs.pattern || field.pattern,
});
};
AutoComplete.props = {
...AutoComplete.props,
pattern: {type: String, optional: true},
};

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">
<t t-inherit="web.AutoComplete" t-inherit-mode="extension" owl="1">
<input position="attributes">
<attribute name="t-att-pattern">props.pattern</attribute>
</input>
</t>
</templates>

View File

@@ -0,0 +1,37 @@
/** @odoo-module **/
import {CharField} from "@web/views/fields/char/char_field";
import {_lt} from "@web/core/l10n/translation";
import {patch} from "@web/core/utils/patch";
import {sprintf} from "@web/core/utils/strings";
patch(CharField.prototype, "web_widget_pattern", {
parse(value) {
const result = this._super(...arguments);
const pattern = this.props.pattern;
if (pattern) {
const regex = new RegExp(pattern, "v");
const match = regex.exec(result);
if (!match || match[0] !== value) {
throw new Error(
_lt(
sprintf("%s does not match required pattern %s", value, pattern)
)
);
}
}
return result;
},
});
const _extractProps = CharField.extractProps;
CharField.extractProps = ({attrs, field}) => {
return Object.assign(_extractProps({attrs, field}), {
pattern: attrs.pattern || field.pattern,
});
};
CharField.props = {
...CharField.props,
pattern: {type: String, optional: true},
};

View File

@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates xml:space="preserve">
<t t-inherit="web.CharField" t-inherit-mode="extension" owl="1">
<input position="attributes">
<attribute name="t-att-pattern">props.pattern</attribute>
</input>
</t>
</templates>