From d0e07a0fc06a83ed45dd7c491e1475e340aa564e Mon Sep 17 00:00:00 2001 From: Fabian Mastenbroek Date: Tue, 10 Nov 2020 22:02:48 +0100 Subject: Perform form validation in modals This change updates the modal components to perform validation before submitting the request. --- .../custom-components/NewPortfolioModalComponent.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'frontend/src/components/modals/custom-components/NewPortfolioModalComponent.js') diff --git a/frontend/src/components/modals/custom-components/NewPortfolioModalComponent.js b/frontend/src/components/modals/custom-components/NewPortfolioModalComponent.js index 67646e2c..3c6b8724 100644 --- a/frontend/src/components/modals/custom-components/NewPortfolioModalComponent.js +++ b/frontend/src/components/modals/custom-components/NewPortfolioModalComponent.js @@ -5,15 +5,23 @@ import Modal from '../Modal' import { AVAILABLE_METRICS, METRIC_NAMES } from '../../../util/available-metrics' const NewPortfolioModalComponent = ({ show, callback }) => { + const form = useRef(null) const textInput = useRef(null) const repeatsInput = useRef(null) const metricCheckboxes = useRef({}) - const onSubmit = () => - callback(textInput.current.value, { - enabledMetrics: AVAILABLE_METRICS.filter((metric) => metricCheckboxes.current[metric].checked), - repeatsPerScenario: parseInt(repeatsInput.current.value), - }) + const onSubmit = () => { + if (form.current.reportValidity()) { + callback(textInput.current.value, { + enabledMetrics: AVAILABLE_METRICS.filter((metric) => metricCheckboxes.current[metric].checked), + repeatsPerScenario: parseInt(repeatsInput.current.value), + }) + + return true + } else { + return false + } + } const onCancel = () => callback(undefined) return ( @@ -23,6 +31,7 @@ const NewPortfolioModalComponent = ({ show, callback }) => { e.preventDefault() this.onSubmit() }} + innerRef={form} > -- cgit v1.2.3