Skip to content

Commit 5e82319

Browse files
brendanbondlane-formio
authored andcommitted
Merge pull request #95 from formio/FIO-8336-fix-validation-on-multiple-values
FIO-8336 fix validation on multiple values
1 parent d5314ae commit 5e82319

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { expect } from 'chai';
2+
import { validationRules } from '..';
3+
import { rules, serverRules } from '../rules';
4+
5+
const allRules = [...rules, ...serverRules];
6+
7+
const component = {
8+
type: 'textfield',
9+
key: 'multiple_textfield',
10+
label: 'Multiple Textfield',
11+
input: true,
12+
multiple: true,
13+
validate: {
14+
required: true,
15+
maxLength: 10,
16+
minLength: 5,
17+
pattern: '^[0-9]+$',
18+
}
19+
};
20+
21+
const context = {
22+
component,
23+
value: [],
24+
path: 'multiple_textfield',
25+
data: {multiple_textfield: []},
26+
row: {multiple_textfield: []},
27+
scope: {errors: []},
28+
};
29+
30+
it('Validating required rule will work for multiple values component with no rows', async () => {
31+
const fullValueRules = allRules.filter((rule) => rule.fullValue);
32+
const rulesToValidate = validationRules(context, fullValueRules, undefined);
33+
expect(rulesToValidate).to.not.have.length(0);
34+
});
35+
36+
it('Validati olther rules will skip for multiple values component with no rows', async () => {
37+
const otherRules = allRules.filter((rule) => !rule.fullValue);
38+
const rulesToValidate = validationRules(context, otherRules, undefined);
39+
expect(rulesToValidate).to.have.length(0);
40+
});

src/process/validation/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ export function validationRules(
4949
}
5050
const validationRules: ValidationRuleInfo[] = [];
5151
return rules.reduce((acc, rule: ValidationRuleInfo) => {
52+
if (context.component.multiple &&
53+
Array.isArray(context.value) &&
54+
context.value?.length === 0 &&
55+
!rule.fullValue
56+
) {
57+
return acc;
58+
}
59+
5260
if (rule.shouldProcess && rule.shouldProcess(context)) {
5361
acc.push(rule);
5462
}

src/process/validation/rules/__tests__/validateMultiple.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('validateMultiple', () => {
3636

3737
it('should return false for textArea component with as !== json', () => {
3838
const component: TextAreaComponent = {
39-
type: 'textArea',
39+
type: 'textarea',
4040
as: 'text',
4141
input: true,
4242
key: 'textArea',

0 commit comments

Comments
 (0)