Form Validator function called infinitely

1 Answer 119 Views
Form
Noah
Top achievements
Rank 1
Noah asked on 29 Dec 2023, 05:57 PM | edited on 29 Dec 2023, 07:04 PM

On kendo-react-form version 7.0.2, passing a validator to the Form component seems to invoke the validator function in an infinite loop.

When I render a form with the following code, my "Form Validation" console log prints to the console infinitely.  It doesn't seem to be a re-render issue, as my "Form Render" console log inside of the Form's render function does not seem to get called infinitely.

The particular form validator function I'm using for the purpose of this example is taken directly from https://www.telerik.com/kendo-react-ui/components/form/validation/#toc-form-validation.  

Can anyone advise me on whether this is the expected behavior, or if there is a problem with kendo-react-form 7.0.2, or if there's something obvious that I'm doing wrong in my implementation?

export const UserForm = (props: UserFormProps) => { const handleResponse = (result: any) => { setServerErrorState(result); if (result.errorDetails?.length > 0 || result.errorMessage) { onSubmitError(result); } else { setServerErrorState(null); onSubmitSuccess(result); } }; const firstNameGetter: any = getter("user.firstName"); const lastNameGetter: any = getter("user.lastName"); const firstOrLastNameValidator = (values: any) => { console.log("Form Validation"); if (firstNameGetter(values) || lastNameGetter(values)) { return; } return { VALIDATION_SUMMARY: "Please fill at least one of the following fields.", ["user.firstName"]: "Please check the validation summary for more information.", ["user.lastName"]: "Please check the validation summary for more information.", }; }; return ( <Form id="test" initialValues={props.editorUser} onSubmitClick={handleSubmit} validator={firstOrLastNameValidator} render={() => { console.log("Form Render"); return ( <FormElement> <div className="form-content-container" style={{ overflowY: "scroll" }}> <UserInformationSection /> //returns Field components wrapped in div
<UserAddressSection />
//returns Field components wrapped in div</div> </FormElement> ); }} /> ); };


Noah
Top achievements
Rank 1
commented on 29 Dec 2023, 08:37 PM

It seems I was mistaken.  It looks like the form actually is re-rendering infinitely, even when there is no form validator.   It seems that the FormElement component does not like custom children components which return multiple fields.  

Is there some way to achieve this functionality, so that sets of form Fields can be grouped into components which can be reused in multiple different forms?

1 Answer, 1 is accepted

Sort by
0
Konstantin Dikov
Telerik team
answered on 01 Jan 2024, 06:21 AM

Hi Noah,

The re-mounting of the Form component can occur if the field components are defined as inline function within the main component with the Form. Defining the components outside of the main component should resolve the issue that you are facing. If you want to pass references from the main component you can use React Context.

If the problem persists, please share a stackblitz example demonstrating the issue, so we can test and debug it locally.

 

Best Regards,
Konstantin Dikov
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Kendo family, check out our getting started resources!

Tags
Form
Asked by
Noah
Top achievements
Rank 1
Answers by
Konstantin Dikov
Telerik team
Share this question
or