AAE-22947 form-field-validator update

This commit is contained in:
wiktord2000
2024-06-11 08:40:46 +02:00
parent a48611eb92
commit 7c2aeb060b
2 changed files with 16 additions and 13 deletions

View File

@@ -16,7 +16,6 @@
*/ */
import { ErrorMessageModel } from './error-message.model'; import { ErrorMessageModel } from './error-message.model';
import { FormFieldOption } from './form-field-option';
import { FormFieldTypes } from './form-field-types'; import { FormFieldTypes } from './form-field-types';
import { import {
FixedValueFieldValidator, FixedValueFieldValidator,
@@ -68,17 +67,20 @@ describe('FormFieldValidator', () => {
it('should fail (display error) for dropdown with empty value', () => { it('should fail (display error) for dropdown with empty value', () => {
const field = new FormFieldModel(new FormModel(), { const field = new FormFieldModel(new FormModel(), {
type: FormFieldTypes.DROPDOWN, type: FormFieldTypes.DROPDOWN,
value: '<empty>', value: null,
options: [{ id: 'empty', name: 'Choose option...' }], options: [
{ id: 'empty', name: 'Choose option...' },
{ id: 'none_empty', name: 'None empty' }
],
hasEmptyValue: true, hasEmptyValue: true,
required: true required: true
}); });
field.emptyOption = { id: '<empty>' } as FormFieldOption; expect(field.value).toEqual({ id: 'empty', name: 'Choose option...' });
expect(validator.validate(field)).toBe(false); expect(validator.validate(field)).toBeFalsy();
field.value = '<non-empty>'; field.value = { id: 'noneEmpty', name: 'None empty' };
expect(validator.validate(field)).toBe(true); expect(validator.validate(field)).toBeTruthy();
}); });
it('should fail (display error) for multiple type dropdown with zero selection', () => { it('should fail (display error) for multiple type dropdown with zero selection', () => {
@@ -107,11 +109,12 @@ describe('FormFieldValidator', () => {
type: FormFieldTypes.DROPDOWN, type: FormFieldTypes.DROPDOWN,
value: null, value: null,
required: true, required: true,
options: [{ id: 'one', name: 'one' }], options: [{ id: 'id_dog', name: 'Dog' }],
selectionType: 'multiple' selectionType: 'multiple',
hasEmptyValue: false
}); });
expect(validator.validate(field)).toBe(false); expect(validator.validate(field)).toBeFalsy();
}); });
it('should fail (display error) for dropdown with empty object', () => { it('should fail (display error) for dropdown with empty object', () => {
@@ -119,7 +122,7 @@ describe('FormFieldValidator', () => {
type: FormFieldTypes.DROPDOWN, type: FormFieldTypes.DROPDOWN,
value: {}, value: {},
required: true, required: true,
options: [{ id: 'one', name: 'one' }], options: [{ id: 'one', name: 'One' }],
selectionType: 'multiple' selectionType: 'multiple'
}); });

View File

@@ -60,8 +60,8 @@ export class RequiredFieldValidator implements FormFieldValidator {
return Array.isArray(field.value) && !!field.value.length; return Array.isArray(field.value) && !!field.value.length;
} }
if (field.hasEmptyValue && field.emptyOption) { if (field.hasEmptyValue && field.emptyValueOption) {
if (field.value === field.emptyOption.id) { if (field.value.id === field.emptyValueOption.id) {
return false; return false;
} }
} }