I was following chapter 3 of the redwood tutorial, that explains how to use components from ‘redwoodjs/forms’ to validate data from html forms. However, I had installed Chakra UI for styling my application. I’m a beginner so maybe I’m missing something, but I don’t see a path forward to get the benefits of validation with ‘redwoodjs/forms’ with the styling of Chakra. I’m happy to help implement a solution with some guidance.
I’ve never tried it myself though. So no idea how it’d work. @keith.t.elliott I know you’ve used Chakra. Any experience with integrating it with rw forms? @jacebenson you “liked” the GH issue comment. Have you tried this?
Either you get redwood’s forms, OR you use react-hook-form with chakra.
You have to include react-hook-form, and pass register to the fields. I feel like my code is a mess and overly complicated, but here’s an example of how I’m making my forms.
I load up all the things I need and pass them into a “FormComponent” and on that, I set the register and the like for each form.
For me the thing that took me the longest to figure out was understanding I needed to “register” the field with react hook form.
Additionally, chakra UI has some documentation on how to use it with react hook form. Since chakra is component-based I don’t think it is possible to use it directly with redwoods automatically registered form inputs.
Basically, Chakra UI has an as prop that can be used to render a Chakra component as something else. In this case, I’m rendering a Chakra Input component as a Redwood TextField component.
In case the as component doesn’t cut it, there’s also the Chakra Factory Function. We usually use the factory function to wrap Next.js’s Image component so we can have Chakra UI styling with the image optimization from Next.js. There’s a shouldForwardProp property that you can define in the options (2nd argument of factory function) if you want to precisely define which props will be forwarded to the other component (ex. the nextjs image).
I think the as prop and/or the Chakra Factory Function will help you integrate Redwood Forms with Chakra UI. Let me know if I should provide more info on this.
To make your Chakra UI forms cleaner you could use: Form - Saas UI
It’s a project I’m building on top of Chakra UI, the forms use Hook Form under the hood and has a similar API as Redwood forms.
If there is any way I can help with implementing Chakra with the Redwood forms, let me know.
Thanks for all the input everyone. I did some testing.
First I converted the form to use chakra elements with the “as=” prop pointing to redwood form components. It styles the whole form correctly and still functions like a redwood form, however, after submitting and receiving errors, the error styling reverts.
I followed the Chakra UI + React Hook Form code. This also styles it correctly, and handles errors client side great, but out of the box it has no way to render redwood service validation on the backend.
Path #1 seems like the most viable option, to keep the redwood forms as close to the ecosystem as possible, it might just require some class/styling updates.
@Eelco my name on discord shane_monk. Happy to talk more there.
@shanemlk the FormControl component controls the error with the isInvalid prop. Try setting the isInvalid prop to the FormControl to the error property for that input in the errors. As the name suggests, it accepts a boolean value.
I updated it to capture the name of the input on error with useState(). It then sets the isInvalid prop on the FormControl. This is working great now with backend validation. It doesn’t display the errors below the input, but it highlights them red, so that’s good enough for me.
@shanemlk I took a stab at this and the reason that the error message is not showing the way you are using it is because FormErrorMessage expects the error message as children so passing the FieldError. Doing it like this worked for me, the className might not be necessary, I was just using it like that.
Hi @aguscha333 Thanks for this. Were you able to get client side validations to show or just server side? I have the following yet it’s not clear how I can trigger isInvalid={true} on the client to display Email is required <===.
Is the suggestion here that one just goes with server side validations?