Skip to content

Commit

Permalink
[TM-532] add new validation to limit text (#931)
Browse files Browse the repository at this point in the history
* [TM-532] add new validation to limit text

* [TM-532] call trigger to field

* [TM-532] update snapshots

* [TM-532] remove useEffect
  • Loading branch information
LimberHope authored Feb 14, 2025
1 parent 154c47b commit 887c0ac
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
FormDataConsumer,
FormDataConsumerRenderParams,
minLength,
NumberInput,
required,
TextInput,
useInput
Expand Down Expand Up @@ -109,6 +110,28 @@ export const QuestionArrayInput = ({
height="75px"
/>
)}
<FormDataConsumer>
{({ scopedFormData, getSource }: FormDataConsumerRenderParams) => {
if (!scopedFormData || !getSource) return null;
const field = getFieldByUUID(scopedFormData.linked_field_key);
return field?.input_type == "long-text" ? (
<>
<NumberInput
source={getSource("min_character_limit")}
label="Minimum Character Limit"
defaultValue={90000}
/>
<NumberInput
source={getSource("max_character_limit")}
label="Maximum Character Limit"
defaultValue={90000}
/>
</>
) : (
<></>
);
}}
</FormDataConsumer>
<BooleanInput
source="validation.required"
label="Required"
Expand Down
2 changes: 1 addition & 1 deletion src/components/elements/Inputs/textArea/TextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const TextArea = ({ formHook, className, onChange: externalOnChange, ...inputWra
<textarea
{...mergedProps}
onChange={handleTextAreaChange}
{...formHook?.register(inputWrapperProps.name)}
{...formHook?.register(inputWrapperProps.name, { onChange: () => formHook?.trigger(inputWrapperProps.name) })}
id={id}
className={inputClasses}
/>
Expand Down
16 changes: 16 additions & 0 deletions src/helpers/customForms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ export const apiFormQuestionToFormField = (
condition: question.show_on_parent_condition,
is_parent_conditional_default: question.is_parent_conditional_default,
parent_id: question.parent_id,
min_character_limit: question.min_character_limit,
max_character_limit: question.max_character_limit,
feedbackRequired
};

Expand Down Expand Up @@ -571,6 +573,8 @@ const getFieldValidation = (question: FormQuestionRead, t: typeof useT, framewor
const required = question.validation?.required || false;
const max = question.validation?.max;
const min = question.validation?.min;
const limitMin = question.min_character_limit;
const limitMax = question.max_character_limit;

switch (question.input_type) {
case "text":
Expand All @@ -590,6 +594,18 @@ const getFieldValidation = (question: FormQuestionRead, t: typeof useT, framewor
if (isNumber(min)) validation = validation.min(min);
if (max) validation = validation.max(max);
if (required) validation = validation.required();
if (limitMin)
validation = validation.min(
limitMin,
t(`Your answer does not meet the minimum required characters ${limitMin} for this field.`)
);
if (limitMax)
validation = validation.max(
limitMax,
t(
`Your answer length exceeds the maximum number of characters ${limitMax} allowed for this field. Please edit your answer to fit within the required number of characters for this field.`
)
);

return validation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "Headquarters address Country",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "3e34aab6-f6fe-45f2-9d2c-581a69dbd1bc",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -2057,6 +2059,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "What Countries is your organisation legally registered in?",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "a90630bf-b77f-4b6e-a828-013f4d0ac1fb",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -2170,6 +2174,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "Organization Mission",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "d64877a6-fc33-4bd8-9f75-43e77a5112ab",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -2224,6 +2230,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "On what date was your organisation founded?",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "ced1d792-2f5f-40b0-8686-86e048c98406",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -2311,6 +2319,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "In what languages can your organisation communicate?",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "6827e8c1-632c-47cf-a45b-43b8bfba78f2",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -2517,6 +2527,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "What interventions do you intend to use to restore land?",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "9bfe6b76-2ff1-4658-9763-54bc6242cce6",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -2632,6 +2644,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "Please provide at least two letters of reference",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "6303cc5c-cbd7-4f46-bed6-d339c4c2f00b",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -2679,6 +2693,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "Organization Twitter URL(optional)",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "981edb54-739e-4971-b078-4e2a971d9ffd",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -2726,6 +2742,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "List the people who have a ownership stake in your company",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "4388ff23-a5e7-486a-9b37-7ad41cce5e77",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -2819,6 +2837,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "How does your organization engage with farmers? ",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "3be9ae6a-c769-44e7-a8e6-46cd9b7d7ff2",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -2975,6 +2995,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "How does your organization engage with women?",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "32873c74-5ec4-4508-90a1-bb05b9139ab2",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -3131,6 +3153,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "How does your organization engage with people younger than 35 years old?",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "bb1c3fd3-be90-46db-9996-5148ead42376",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -3253,6 +3277,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "What were your organization's revenues in USD in 2020?",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "2fc2ddcb-65c4-401f-ad8b-2464127995e1",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -3308,6 +3334,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "Please upload your organization's 2020 income statement and balance sheet.",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "dfc8b46f-a2b5-4c47-9d86-6eaf02f38ff4",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -3355,6 +3383,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "What were your organization's revenues in USD in 2021?",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "a5ff9d12-249f-4752-8c56-93d6c21ff7b6",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -3410,6 +3440,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "Please upload your organization's 2021 income statement and balance sheet.",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "4519d674-4110-4523-a745-f999741622cd",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -3457,6 +3489,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "What were your organization's revenues in USD in 2022?",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "c1537262-4802-4223-b694-9088d76a2d11",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -3512,6 +3546,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "Please upload your organization's 2022 income statement and balance sheet.",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "eae46eca-8e4e-4cec-a7a0-cd9d93af8054",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -3559,6 +3595,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "What are your organization's projected revenues in USD for 2023",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "ae621f3b-552c-4f5f-8616-5d7821299959",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -3612,6 +3650,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "How many years of restoration experience does your organization have?",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "003a59f0-d262-45b1-b4ef-c46c62f79341",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -3659,6 +3699,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "How many hectares of degraded land has your organisation restored since it was founded? ",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "c8ba24a7-3532-4665-99d0-8d1b10e85f27",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -3706,6 +3748,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "How many hectares of degraded land has your organization restored in the past 36 months?",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "12e2645b-ab2d-4c69-ab21-379408a1e743",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -3753,6 +3797,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "How many trees has your organization restored or naturally regenerated since it was founded? ",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "1817a476-e340-467c-8c6b-8918c200654e",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -3800,6 +3846,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "How many trees has your organization planted, naturally regenerated or otherwise restored in the past 36 months?",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "4c0e0a9f-636a-45b0-ad5c-80f5722c7cfe",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -3853,6 +3901,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "What is the name of your proposed project?",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "2f114af5-a0b9-405d-926b-7689a9fd5a25",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -3897,6 +3947,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "What are the objectives of your proposed project?",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "917fd2b0-74c2-42c8-b65e-b98144322f6d",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -4940,6 +4992,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "In what country will your project operate? ",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "875d923a-f1e4-4516-a77a-96fcdf099f66",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -4987,6 +5041,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "In which subnational jurisdictions would you carry out this project? ",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "59388280-52bd-48f8-97a6-8de30b509494",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -5034,6 +5090,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "How many hectares of land do you intend to restore through this project?",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "5c2b9303-302d-4140-beba-95d79fb2d060",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -5081,6 +5139,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "How many trees do you intend to restore through this project?",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "790370ce-a8e4-4185-a6ef-99a64b2e4bc5",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -5127,6 +5187,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "What tree species do you intend to grow through this project?",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "90acff1b-27f1-4de7-a9a3-caf6be137604",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -5372,6 +5434,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "What is your proposed project budget in USD? ",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "09475088-63b9-4634-bb13-d0ad6c9ecf14",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -5436,6 +5500,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "Upload any additional documents",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "e55c81f1-e0ba-42a3-9887-d22f853687c2",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -5633,6 +5699,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "On which of the following topics would you request technical assistance from a team of experts?",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "3d456c52-8e68-45ff-ae88-37a96263130e",
"parent_id": undefined,
"placeholder": undefined,
Expand Down Expand Up @@ -5899,6 +5967,8 @@ exports[`test useGetCustomFormSteps hook snapShot test 1`] = `
},
"is_parent_conditional_default": undefined,
"label": "How did you hear about this opportunity on TerraMatch?",
"max_character_limit": undefined,
"min_character_limit": undefined,
"name": "6ed56eff-b1c3-4df3-bfcc-98db47e02fb0",
"parent_id": undefined,
"placeholder": undefined,
Expand Down

0 comments on commit 887c0ac

Please sign in to comment.