-
-
Notifications
You must be signed in to change notification settings - Fork 56
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
How to report Error with Checkboxes? #264
Comments
Please have a look at this guide and let me know if it solves your problem: https://modularforms.dev/solid/guides/field-arrays This code example might help you too: https://stackblitz.com/edit/modular-forms-solid?file=src/routes/todos.tsx |
Using I was looking very closely at: https://stackblitz.com/edit/modular-forms-solid?file=src/routes/special.tsx As a result, I crafted a small project that demonstrates the problem. It uses |
Please provide a StackBlitz link with a minimal reproduction if you want me to take a look at it. |
Here you go: https://stackblitz.com/edit/github-wjzn2o?file=README.md |
Here is a first fix: https://stackblitz.com/edit/github-wjzn2o-tgg3qn?file=src%2Fcomponents%2FConfigureForm.tsx%3AL109 Feel free to let me know your expected behaviour and I will try to teak it out for you. |
That's interesting. If you consider that there are field errors and form errors, I guess I should have an overall form error displayed too. However, I wouldn't want to duplicate the field error like "missing project name" -- I see that it isn't, but I'm confused because it is outside the Field and For loop. In other words, how is "Project Name is required." not part of Having said that, I also expect the "bad checkbox" value would be marked as an error too. I made a tiny tweak to the code: I simply added an explicit style to the label when there is a field error: <div style="padding-left: 1em">
<label style={field.error ? 'color: red' : ''}>
{item}
<input
{...props}
name="uiFrameworks"
type="checkbox"
value={item}
checked={field.value?.includes(item)}
class={`checkbox ${field.error ? 'checkbox-error' : ''}`}
/>
</label>
{field.error && <div style="color: red">{field.error}</div>}
</div> My issue is that You can see that the error messages are part of In other words, what I'm expecting to see is something like:
|
Only error messages that are not related to a specific field are added to the form response.
Here is a fix for only showing the error once: https://stackblitz.com/edit/github-wjzn2o-uqcajv?file=src%2Fcomponents%2FConfigureForm.tsx
This only works if you use |
I don't understand how to use a Field Array for a checkbox. Perhaps you can share an example? |
I think you're right, and my field array idea was a dump idea and can't be applied to this use case. Sorry about that. Do any of the above solutions work for you? |
I'm going with your advice around One thing I don't like is that my actual form is quite long (you need scroll vertically) -- the submit button is at the end. If I input invalid data into a text field, the form will scroll to the bad field. However, if I have a checkbox issue, the error will appear in red, but the user isn't scrolled to that area of the form. Again, I added a workaround, but ideally that would be better too. Note that I am using |
Another workaround would be to add the checkboxes as booleans instead of an array of strings. That way each checkbox is its own field with its own error message. So the auto-scrolling feature works for all fields except the checkboxes? |
I'm unclear on how the auto-scroll is programmed. Is that a Modular Forms feature? But yeah, it isn't auto scrolling to the checkbox field with errors. I'm unclear why not. |
It just calls |
I am using Solid and I have a set of checkboxes:
There is a unique Valibot 🤖 rule that prevents certain combinations from being acceptable.
Specifically, you are unable to choose more than one of Preact, React, and Solid (because they have unique JSX configurations, so you may choose just one).
(These are checkboxes, and not radio buttons, because many combinations are acceptable.)
I have updated Valibot with a rule to prevent bad data. Rule looks something like:
I am pretty sure that Valibot is working because I tested the above in the playground, and I can prevent the form from submitting bad combinations.
However, I am struggling to provide error feedback back to the user.
Consider code like:
For broader context, this is what I am doing for the form:
It is weird in that the submit doesn't seem to trigger a re-render of the checkboxes.
And I've never seen
field.error
not be a blank string.I double checked the docs and playground example, but I don't see a scenario talking about checkbox validation -- at least beyond a simple example like, "Click this to accept Terms and Conditions".
I appreciate any help. Thanks!
The text was updated successfully, but these errors were encountered: