From a2ff59f32d02bc5fb4522a979f341a6504c76b87 Mon Sep 17 00:00:00 2001 From: rdbsolutions Date: Mon, 7 Oct 2024 11:21:16 +0100 Subject: [PATCH] validate required on submit not blur --- components/myportal/searchcases.js | 33 ++++++++++++++++-------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/components/myportal/searchcases.js b/components/myportal/searchcases.js index e30c7908..f815e59b 100644 --- a/components/myportal/searchcases.js +++ b/components/myportal/searchcases.js @@ -2,7 +2,7 @@ import useTranslation from "next-translate/useTranslation"; import Link from "next/link"; import { useRouter } from "next/router"; import { connect } from "react-redux"; -import { Field, reduxForm } from "redux-form"; +import { Field, reduxForm, SubmissionError } from "redux-form"; import { sendGAEvent } from "@next/third-parties/google"; const required = (errorMsg) => (value) => @@ -84,15 +84,23 @@ let CaseSearch = (props) => { // }; const onHandleSubmit = (values) => { - //spinnerState(); - router.push({ - pathname: - router.locale == "cy" - ? "/fymhorth/canlyniadauchwilio" - : "/myportal/searchresults", - query: { q: values.basicSearch }, - shallow: true, - }); + if (Object.keys(values).length === 0) { + console.log(props.error); + + throw new SubmissionError({ + basicSearch: t("myportal:searchcases-validation-required"), + _error: "required", + }); + } else { + router.push({ + pathname: + router.locale == "cy" + ? "/fymhorth/canlyniadauchwilio" + : "/myportal/searchresults", + query: { q: values.basicSearch }, + shallow: true, + }); + } }; return ( @@ -106,11 +114,6 @@ let CaseSearch = (props) => {