Skip to content

Commit

Permalink
fix: validate multi-select field
Browse files Browse the repository at this point in the history
  • Loading branch information
fterra-encora committed Oct 17, 2023
1 parent 6f40376 commit 244f76c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
9 changes: 2 additions & 7 deletions frontend/src/components/forms/MultiselectInputComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,13 @@ const validateInput = (newValue: any) => {
if (message) return true;
return false;
};
const validate = (value: string) =>
const validate = (value: string[]) =>
props.validations
.map((validation) => validation(value))
.filter(hasError)
.shift() ?? props.errorMessage;
error.value =
items.value
.map((item: string) => validate(item))
.filter(hasError)
.shift() ?? props.errorMessage;
error.value = validate(items.value);
}
};
Expand Down Expand Up @@ -113,7 +109,6 @@ watch(
() => (error.value = props.errorMessage)
);
validateInput(selectedValue.value);
revalidateBus.on(() => validateInput(selectedValue.value));
</script>

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/grouping/ContactGroupComponent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const updateContactType = (value: CodeNameType | undefined) => {
:model-value="addressList"
:selectedValues="selectedValue.locationNames?.map((location:CodeDescrType) => location?.text)"
:validations="[
...getValidations('location.contacts.*.locationNames.*.text'),
...getValidations('location.contacts.*.locationNames'),
submissionValidation(`location.contacts[${id}].locationNames`)
]"
@update:selected-value="
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/helpers/validators/BCeIDFormValidations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
isNoSpecialCharacters,
isNot,
formFieldValidations,
isNotEmptyArray,
} from "@/helpers/validators/GlobalValidators";


Expand Down Expand Up @@ -70,8 +71,8 @@ formFieldValidations[
];

// Step 3: Contacts
formFieldValidations["location.contacts.*.locationNames.*.text"] = [
isNotEmpty("You must select at least one location"),
formFieldValidations["location.contacts.*.locationNames"] = [
isNotEmptyArray("You must select at least one location"),
];
formFieldValidations["location.contacts.*.contactType.text"] = [
isNotEmpty("You must select at least one contact type"),
Expand Down
17 changes: 17 additions & 0 deletions frontend/src/helpers/validators/GlobalValidators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ export const isNotEmpty =
return message;
};

/**
* Checks if the array is not empty
* @param array - The array to check
* @returns An empty string if the array is not empty, error message otherwise
* @example
* isNotEmptyArray([]) // false
* isNotEmptyArray('a') // true
* isNotEmptyArray([' ']) // true
* isNotEmptyArray([undefined]) // true
*/
export const isNotEmptyArray =
(message: string = "This field is required") =>
(array: any[]): string => {
if (array && array.length > 0) return "";
return message;
};

/**
* Checks if the value is an email
* @param value - The value to check
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import MultiselectInputComponent from '@/components/forms/MultiselectInputCompon

describe('MultiselectInputComponent', () => {
const validations = [
(value: any) =>
value === 'A' || value === 'Value A' ? 'A is not supported' : ''
(value: any[]) =>
value.includes('A') || value.includes('Value A') ? 'A is not supported' : ''
]

const eventContent = (value:string) => {return { data: value }}

const eventContent = (value: string) => {
return { data: value }
}

it('should render', () => {
const wrapper = mount(MultiselectInputComponent, {
Expand Down Expand Up @@ -128,7 +129,7 @@ describe('MultiselectInputComponent', () => {

const dropdown = wrapper.find('cds-multi-select')

await dropdown.trigger('cds-multi-select-selected',eventContent('Value A'))
await dropdown.trigger('cds-multi-select-selected', eventContent('Value A'))

expect(wrapper.emitted('update:modelValue')).toBeTruthy()
expect(wrapper.emitted('update:modelValue')![0][0]).toStrictEqual(['Value A'])
Expand All @@ -142,7 +143,7 @@ describe('MultiselectInputComponent', () => {
])

expect(wrapper.emitted('error')).toBeTruthy()
expect(wrapper.emitted('error')![1][0]).toBe('A is not supported')
expect(wrapper.emitted('error')![0][0]).toBe('A is not supported')
})

it('should validate and emit no error if required', async () => {
Expand Down

0 comments on commit 244f76c

Please sign in to comment.