-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider adding a way to get/list all the fields and iterate them ? #10
Comments
Actually this is already ReForm does internally, the trick is to iterate over the validation schema! To have a way to iterate over the list of GADTs directly is very cumbersome, see here https://discuss.ocaml.org/t/packing-gadt-constructors-in-iterable-data-structure/2678/4 |
Sorry for the late response 😅 |
I've been using this pattern: let mergeWholeState =
(
formState,
~schema:
ReSchema.Make(OnboardingProfileFormTypes.StateLenses).Validation.schema(
option(ProfileFastForm.meta),
),
) => {
open OnboardingProfileFormTypes.StateLenses;
let Schema(schema) = schema;
let changeField = field => {
let currentState = currentWholeFormState->React.Ref.current;
let newState = currentState->set(field, formState->get(field));
currentWholeFormState->React.Ref.setCurrent(newState);
};
schema->Belt.Array.forEach(validator => {
switch (validator) {
| ProfileFastForm.Form.Validation.StringNonEmpty({field})
| ProfileFastForm.Form.Validation.StringRegExp({field})
| ProfileFastForm.Form.Validation.StringMin({field})
| ProfileFastForm.Form.Validation.StringMax({field})
| ProfileFastForm.Form.Validation.Email({field}) =>
let currentState = currentWholeFormState->React.Ref.current;
let newState =
currentState->set(field, formState->get(field)->Js.String.trim);
currentWholeFormState->React.Ref.setCurrent(newState);
| ProfileFastForm.Form.Validation.IntMin({field})
| ProfileFastForm.Form.Validation.IntMax({field}) => changeField(field)
| ProfileFastForm.Form.Validation.FloatMin({field})
| ProfileFastForm.Form.Validation.FloatMax({field}) =>
changeField(field)
| ProfileFastForm.Form.Validation.Custom({field}) => changeField(field)
| ProfileFastForm.Form.Validation.NoValidation({field}) =>
changeField(field)
}
});
Hooks.OnboardingFormData.persist(currentWholeFormState->React.Ref.current)
|> ignore;
}; though the list of validators is pretty huge it's mostly boilerplate and if you are using ocamllsp you can use the |
Sometimes when working with forms one might want to loop over all the fields and render any errors they have.
Perhaps this library could generate some kind utilities for this in addition to getters&setters of individual fields?
For example maybe I'd like to do this in
onSubmitFail
of reforms:however, the code can get quite tedious quite fast with larger forms.
Or maybe I should be approaching this problem differently?
The text was updated successfully, but these errors were encountered: