From ecf1d79ca50178ba9ef931863f9eae836c022fcf Mon Sep 17 00:00:00 2001 From: Keith Grant Date: Thu, 19 Dec 2019 14:55:48 -0800 Subject: [PATCH] fix form validation for Organization select in Inventory form --- .../src/screens/Inventory/shared/InventoryForm.jsx | 2 ++ awx/ui_next/src/util/validators.jsx | 9 ++++++++- awx/ui_next/src/util/validators.test.js | 10 ++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/awx/ui_next/src/screens/Inventory/shared/InventoryForm.jsx b/awx/ui_next/src/screens/Inventory/shared/InventoryForm.jsx index 3f186431ec..7bacd2d3a5 100644 --- a/awx/ui_next/src/screens/Inventory/shared/InventoryForm.jsx +++ b/awx/ui_next/src/screens/Inventory/shared/InventoryForm.jsx @@ -77,6 +77,8 @@ function InventoryForm({ form.setFieldValue('organization', value); }} value={field.value} + touched={form.touched.organization} + error={form.errors.organization} required /> )} diff --git a/awx/ui_next/src/util/validators.jsx b/awx/ui_next/src/util/validators.jsx index 52edfd3f40..fe4627a935 100644 --- a/awx/ui_next/src/util/validators.jsx +++ b/awx/ui_next/src/util/validators.jsx @@ -1,9 +1,16 @@ import { t } from '@lingui/macro'; export function required(message, i18n) { + const errorMessage = message || i18n._(t`This field must not be blank`); return value => { if (typeof value === 'string' && !value.trim()) { - return message || i18n._(t`This field must not be blank`); + return errorMessage; + } + if (typeof value === 'number' && !Number.isNaN(value)) { + return undefined; + } + if (!value) { + return errorMessage; } return undefined; }; diff --git a/awx/ui_next/src/util/validators.test.js b/awx/ui_next/src/util/validators.test.js index 2fec22ec15..ceb5eecc0f 100644 --- a/awx/ui_next/src/util/validators.test.js +++ b/awx/ui_next/src/util/validators.test.js @@ -27,6 +27,16 @@ describe('validators', () => { }); }); + test('required interprets undefined as empty value', () => { + expect(required(null, i18n)(undefined)).toEqual({ + id: 'This field must not be blank', + }); + }); + + test('required interprets 0 as non-empty value', () => { + expect(required(null, i18n)(0)).toBeUndefined(); + }); + test('maxLength accepts value below max', () => { expect(maxLength(10, i18n)('snazzy')).toBeUndefined(); });