mirror of
https://github.com/element-hq/element-web
synced 2024-11-24 18:25:49 +03:00
refactor to add Validation::final
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
parent
3e85f65d9e
commit
8aaab0a3ba
2 changed files with 11 additions and 9 deletions
|
@ -38,13 +38,6 @@ export default class RoomAliasField extends React.PureComponent {
|
||||||
return `#${localpart}:${this.props.domain}`;
|
return `#${localpart}:${this.props.domain}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
_isValid(value) {
|
|
||||||
const fullAlias = this._asFullAlias(value);
|
|
||||||
// XXX: FIXME https://github.com/matrix-org/matrix-doc/issues/668
|
|
||||||
return !value.includes("#") && !value.includes(":") && !value.includes(",") &&
|
|
||||||
encodeURI(fullAlias) === fullAlias;
|
|
||||||
}
|
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const Field = sdk.getComponent('views.elements.Field');
|
const Field = sdk.getComponent('views.elements.Field');
|
||||||
const poundSign = (<span>#</span>);
|
const poundSign = (<span>#</span>);
|
||||||
|
@ -87,7 +80,10 @@ export default class RoomAliasField extends React.PureComponent {
|
||||||
if (!value) {
|
if (!value) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return this._isValid(value);
|
const fullAlias = this._asFullAlias(value);
|
||||||
|
// XXX: FIXME https://github.com/matrix-org/matrix-doc/issues/668
|
||||||
|
return !value.includes("#") && !value.includes(":") && !value.includes(",") &&
|
||||||
|
encodeURI(fullAlias) === fullAlias;
|
||||||
},
|
},
|
||||||
invalid: () => _t("Some characters not allowed"),
|
invalid: () => _t("Some characters not allowed"),
|
||||||
}, {
|
}, {
|
||||||
|
@ -96,7 +92,7 @@ export default class RoomAliasField extends React.PureComponent {
|
||||||
invalid: () => _t("Please provide a room alias"),
|
invalid: () => _t("Please provide a room alias"),
|
||||||
}, {
|
}, {
|
||||||
key: "taken",
|
key: "taken",
|
||||||
skip: ({value}) => !value || !this._isValid(value),
|
final: true,
|
||||||
test: async ({value}) => {
|
test: async ({value}) => {
|
||||||
if (!value) {
|
if (!value) {
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -28,9 +28,11 @@ import classNames from 'classnames';
|
||||||
* An array of rules describing how to check to input value. Each rule in an object
|
* An array of rules describing how to check to input value. Each rule in an object
|
||||||
* and may have the following properties:
|
* and may have the following properties:
|
||||||
* - `key`: A unique ID for the rule. Required.
|
* - `key`: A unique ID for the rule. Required.
|
||||||
|
* - `skip`: A function used to determine whether the rule should even be evaluated.
|
||||||
* - `test`: A function used to determine the rule's current validity. Required.
|
* - `test`: A function used to determine the rule's current validity. Required.
|
||||||
* - `valid`: Function returning text to show when the rule is valid. Only shown if set.
|
* - `valid`: Function returning text to show when the rule is valid. Only shown if set.
|
||||||
* - `invalid`: Function returning text to show when the rule is invalid. Only shown if set.
|
* - `invalid`: Function returning text to show when the rule is invalid. Only shown if set.
|
||||||
|
* - `final`: A Boolean if true states that this rule will only be considered if all rules before it returned valid.
|
||||||
* @returns {Function}
|
* @returns {Function}
|
||||||
* A validation function that takes in the current input value and returns
|
* A validation function that takes in the current input value and returns
|
||||||
* the overall validity and a feedback UI that can be rendered for more detail.
|
* the overall validity and a feedback UI that can be rendered for more detail.
|
||||||
|
@ -52,6 +54,10 @@ export default function withValidation({ description, rules }) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!valid && rule.final) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
const data = { value, allowEmpty };
|
const data = { value, allowEmpty };
|
||||||
|
|
||||||
if (rule.skip && rule.skip.call(this, data)) {
|
if (rule.skip && rule.skip.call(this, data)) {
|
||||||
|
|
Loading…
Reference in a new issue