Description
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:
export const uiFrameworks = ["preact", "react", "solid", "svelte", "vue"] as const
export const jsxFrameworks = uiFrameworks.filter((framework) => ["preact", "react", "solid"].includes(framework))
...
uiFrameworks: v.pipe(
v.array(v.picklist(uiFrameworks, "Invalid UI Framework.")),
v.checkItems(
(item, index, array) =>
!jsxFrameworks.includes(item) || array.filter((value) => jsxFrameworks.includes(value)).length <= 1,
"Multiple JSX based Frameworks are not allowed.",
),
),
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:
<div class="mt-4">
<div class="label">
<span class="label-text font-bold md:text-lg">Which user interface frameworks would you like to use?</span>
</div>
<For each={uiFrameworks}>
{(item) => (
<Field name="uiFrameworks" type="string[]">
{(field, props) => (
<div class="form-control max-w-64">
<label class="label mx-10 cursor-pointer">
<span class="label-text">{capitalCase(item)}</span>
<input
{...props}
name="uiFrameworks"
type="checkbox"
value={item}
checked={field.value?.includes(item)}
class={`checkbox ${field.error ? "checkbox-error" : ""}`}
/>
</label>
{field.error && (
<div class="label">
<span class="label-text-alt text-error">{field.error}</span>
</div>
)}
</div>
)}
</Field>
)}
</For>
For broader context, this is what I am doing for the form:
export default function ConfigureForm() {
const [configurationForm, { Form, Field }] = createForm<ConfigurationForm>({
validate: valiForm(ConfigurationSchema),
})
const handleSubmit = async (values: ConfigurationForm) => {
console.log("Form passed client validation with values:", values)
const { data, error } = await actions.submitOrder(values)
if (data?.success && data?.redirect) {
navigate(data?.redirect)
}
}
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!