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

View File

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