Hello,
I recently asked a question about an infinite form re-render, and I was able to track it down to a problem misusing the Field component's validator prop. Unfortunately, I'm struggling to achieve the desired functionality using kendo-react's recommended usage.
We'd like to be able to have a set of reusable and minimally customizable validators, for example, a length validator with a configurable maxLength value.
Here's what we have so far:
importReactfrom'react';
import { Field, FieldArray, Form, FormElement, FormRenderProps, FormSubmitClickEvent, FormValidatorType, KeyValue } from'@progress/kendo-react-form';
import { lengthValidator } from'./validators.tsx'exportconstUserForm = (props: UserFormProps) => {
return (
<Form
render={() => {
console.log("Form Render");
return (
<FormElement>
<div className="form-content-container" style={{ overflowY: "scroll" }}>
<Field
name="test"
component={FormInput}
validator={lengthValidator(10)}
/>
</div>
</FormElement>
);
}}
/>
);
};
export const lengthValidator = (maxLength: number) => {
return (value: any, _valueGetter: (name: string) => any) => {
if ( value ) {
const valueStripped: string = value.replace(stripMaskRegex,"");
if ( valueStripped.length > maxLength ) {
return " Cannot exceed length " + maxLength;
}
}
return undefined;
};
};
Here is a stackblitz session demonstrating what we're wanting to do. As you can see, it causes an infinite loop, as demonstrated by the re-render log in the Form's render function.
https://stackblitz-starters-oys53t.stackblitz.io
Obviously we are not doing this in the correct way, but I'm hoping someone can show me that there is a way to correctly create reusable validators that can take configuration arguments like this maxLength on a per-field basis.
Thanks!