ConvertFieldAPIRulesToJoi.ts
7 months ago
PrepareFormData.ts
2 years ago
amountFormatter.ts
2 years ago
buildRegisterValidationOptions.ts
2 years ago
conditionOperatorFunctions.js
2 years ago
convertValuesToFormData.ts
2 years ago
createValidationObjectFromFields.js
2 years ago
generateRequestErrors.ts
1 year ago
getCurrentFormUrlData.js
1 year ago
getDefaultValuesFromSections.ts
2 years ago
getDonationFormNodeSettings.ts
2 years ago
getErrorByFieldName.ts
2 years ago
getWindowData.ts
2 years ago
groups.ts
2 years ago
handleFormRedirect.ts
1 year ago
handleFormSubmitRequest.ts
1 year ago
handleValidationRequest.ts
2 years ago
isRouteInlineRedirect.ts
2 years ago
memoNode.ts
2 years ago
mountWindowData.ts
1 year ago
postData.ts
2 years ago
postFormData.ts
2 years ago
registerFieldAndBuildProps.tsx
1 year ago
setDesignSettings.ts
2 years ago
useDonationFormPubSub.ts
1 year ago
handleValidationRequest.ts
50 lines
| 1 | import {Gateway, isFormResponseGatewayError, isFormResponseValidationError} from '@givewp/forms/types'; |
| 2 | import generateRequestErrors from '../utilities/generateRequestErrors'; |
| 3 | import FormRequestError from '../errors/FormRequestError'; |
| 4 | |
| 5 | import {__} from '@wordpress/i18n'; |
| 6 | import {FieldValues, UseFormSetError} from 'react-hook-form'; |
| 7 | import postFormData from '@givewp/forms/app/utilities/postFormData'; |
| 8 | import convertValuesToFormData from '@givewp/forms/app/utilities/convertValuesToFormData'; |
| 9 | |
| 10 | /** |
| 11 | * @since 3.0.0 |
| 12 | */ |
| 13 | export default async function handleValidationRequest( |
| 14 | validateUrl: string, |
| 15 | values: FieldValues, |
| 16 | setError: UseFormSetError<FieldValues>, |
| 17 | gateway?: Gateway |
| 18 | ) { |
| 19 | if (gateway !== undefined && values?.donationType === 'subscription' && !gateway.supportsSubscriptions) { |
| 20 | return setError('FORM_ERROR', { |
| 21 | message: __( |
| 22 | 'This payment gateway does not support recurring payments, please try selecting another payment gateway.', |
| 23 | 'give' |
| 24 | ), |
| 25 | }); |
| 26 | } |
| 27 | |
| 28 | try { |
| 29 | const formData = convertValuesToFormData(values); |
| 30 | |
| 31 | const {response} = await postFormData(validateUrl, formData); |
| 32 | |
| 33 | const formResponse = await response.json(); |
| 34 | |
| 35 | if (isFormResponseGatewayError(formResponse) || isFormResponseValidationError(formResponse)) { |
| 36 | throw new FormRequestError(formResponse.data.errors.errors); |
| 37 | } else { |
| 38 | return true; |
| 39 | } |
| 40 | } catch (error) { |
| 41 | if (error instanceof FormRequestError) { |
| 42 | return generateRequestErrors(values, error.errors, setError); |
| 43 | } |
| 44 | |
| 45 | return setError('FORM_ERROR', { |
| 46 | message: error?.message ?? __('Something went wrong, please try again or contact support.', 'give'), |
| 47 | }); |
| 48 | } |
| 49 | } |
| 50 |