Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

Commit

Permalink
FDS-665 validation rules logic updated (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
vikas-cldcvr authored Apr 22, 2024
1 parent ad912ec commit 6107154
Show file tree
Hide file tree
Showing 6 changed files with 312 additions and 273 deletions.
6 changes: 6 additions & 0 deletions packages/flow-form-builder/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

# Change Log

## [2.4.1] - 2024-04-22

### Bug Fixes

- Validation rules are triggered multiple times when multiple fields utilize the `showWhen` property.

## [2.4.0] - 2024-03-27

### Features
Expand Down
2 changes: 1 addition & 1 deletion packages/flow-form-builder/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ollion/flow-form-builder",
"version": "2.4.0",
"version": "2.4.1",
"description": "Form builder for the flow design system",
"module": "dist/flow-form-builder.es.js",
"main": "dist/flow-form-builder.cjs.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { FDiv, FRoot } from "@ollion/flow-core";
import { Ref, createRef } from "lit/directives/ref.js";
import fieldRenderer from "./fields";
import { extractValidationState, validateField } from "../../modules/validation/validator";

import { debounce } from "lodash-es";
import { Subject } from "rxjs";
import { getEssentialFlowCoreStyles, propogateProperties } from "../../modules/helpers";
import { cloneDeep, isEqual } from "lodash-es";
Expand Down Expand Up @@ -122,11 +122,14 @@ export class FFormBuilder extends FRoot {
*/
render() {
return html`
<!--debounce added to reduce multiple calls while multiple showWhen executed
It will not affect functionality as such we are just updating silent validation state in single call
-->
<f-form
name=${this.name}
@submit=${this.onSubmit}
@show-when=${this.onShowWhen}
@show-when-exe=${this.onShowWhenExecution}
@show-when-exe=${debounce(this.onShowWhenExecution, 1000)}
?separator=${this.separator}
gap=${ifDefined(this.gap)}
@keyup=${this.handleKeyUp}
Expand Down
58 changes: 34 additions & 24 deletions packages/flow-form-builder/src/modules/validation/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
CanValidateFields,
FFormInputElements,
FormBuilderAsyncValidatorFunction,
FormBuilderCustomValidationRule,
FormBuilderField,
FormBuilderGenericValidationRule,
FormBuilderLabel,
Expand All @@ -27,10 +26,13 @@ import {
} from "@ollion/flow-core";
import defaultValidations from "./default-validations";
import { render } from "lit-html";

type FormBuilderValidationRuleWithPrivateFields = {
_lastValue?: string;
_lastResult?: boolean;
} & FormBuilderGenericValidationRule;
export default async function validate(
value: string,
elementRules: FormBuilderValidationRules,
elementRules: FormBuilderValidationRuleWithPrivateFields[],
name: string,
element: FFormInputElements | FInputLight | undefined
) {
Expand All @@ -39,23 +41,30 @@ export default async function validate(
let rule!: FormBuilderGenericValidationRule["name"];
if (elementRules) {
for (const r of elementRules) {
if (r.name !== "custom") {
result = rules[r.name](value, r.params);
/**
* This will avoid validation rules called multiple time in silent validation
*/
if (r._lastValue === value && r._lastResult !== undefined) {
result = r._lastResult;
if (!result) {
rule = r.name;
message = getValidationMessage(r, { name, value });
break;
}
} else {
if (isAsync(r.validate)) {
const asyncR = r as {
_lastValue?: string;
_lastResult?: boolean;
} & FormBuilderCustomValidationRule;
// this if statement is added to avoid multiple validation calls
if (asyncR._lastValue === value && asyncR._lastResult !== undefined) {
result = asyncR._lastResult;
} else {
r._lastValue = value;
if (r.name !== "custom") {
result = rules[r.name](value, r.params);
r._lastResult = result;
if (!result) {
rule = r.name;
message = getValidationMessage(r, { name, value });
break;
}
} else {
if (isAsync(r.validate)) {
// this if statement is added to avoid multiple validation calls

if (
element instanceof FInput ||
element instanceof FInputLight ||
Expand All @@ -67,9 +76,9 @@ export default async function validate(
element.loading = true;
}
// holding last value
asyncR._lastValue = value;
result = await asyncR.validate(value, { ...asyncR.params, element });
asyncR._lastResult = result;

result = await r.validate(value, { ...r.params, element });
r._lastResult = result;
if (
element instanceof FInput ||
element instanceof FInputLight ||
Expand All @@ -80,14 +89,15 @@ export default async function validate(
) {
element.loading = false;
}
} else {
result = r.validate(value, { ...r.params, element }) as boolean;
r._lastResult = result;
}
if (!result) {
rule = r.name;
message = getValidationMessage(r, { name, value });
break;
}
} else {
result = r.validate(value, { ...r.params, element }) as boolean;
}
if (!result) {
rule = r.name;
message = getValidationMessage(r, { name, value });
break;
}
}
}
Expand Down
246 changes: 0 additions & 246 deletions stories/flow-form-builder/validations.stories.js

This file was deleted.

Loading

0 comments on commit 6107154

Please sign in to comment.