Skip to content

Commit

Permalink
Merge pull request #609 from open-formulieren/fix/3647-invalid-time-c…
Browse files Browse the repository at this point in the history
…rashes-form

[#3647] Prevent invalid time from crashing form
  • Loading branch information
sergei-maertens authored Dec 8, 2023
2 parents f94ba2d + b4d8cf5 commit 0ba181e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/formio/components/TimeField.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ class TimeField extends Time {
].join(' ');
return info;
}

getStringAsValue(value) {
const result = super.getStringAsValue(value);
if (result === 'Invalid date') return value;

return result;
}

getValueAsString(value) {
const result = super.getValueAsString(value);
if (result === 'Invalid date') return value;

return result;
}
}

export default TimeField;
48 changes: 48 additions & 0 deletions src/formio/components/TimeField.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {screen, waitFor} from '@testing-library/dom';
import userEvent from '@testing-library/user-event';
import _ from 'lodash';
import {Formio} from 'react-formio';

import OpenFormsModule from 'formio/module';

// Use our custom components
Formio.use(OpenFormsModule);

const phoneForm = {
type: 'form',
components: [
{
key: 'time',
type: 'time',
input: true,
label: 'Time',
inputType: 'text',
},
],
};

const renderForm = async () => {
let formJSON = _.cloneDeep(phoneForm);
const container = document.createElement('div');
document.body.appendChild(container);
const form = await Formio.createForm(container, formJSON);
return {form, container};
};

describe('The time component', () => {
afterEach(() => {
document.body.innerHTML = '';
});

test('Time component with invalid time', async () => {
const user = userEvent.setup({delay: 50});
const {form} = await renderForm();

const input = screen.getByLabelText('Time');
expect(input).toBeVisible();
await user.type(input, '25:00');
expect(input).toHaveDisplayValue('25:00');

expect(form.isValid()).toBe(false);
});
});

0 comments on commit 0ba181e

Please sign in to comment.