The MarketingForm component's handleSubmit logic incorrectly enforces groupRequired only among checkboxes that themselves have groupRequired: true, and skips individual required validation for grouped checkboxes. This causes a false validation failure when a group mixes groupRequired flags and silently drops required enforcement for grouped checkboxes with required: true.
The group validation builds its group map using only fields where groupRequired is true, instead of all fields in the group, and the individual required filter excludes grouped fields with `!f.group`, contradicting the schema documentation.
1. Define a form schema with three checkboxes in the same group: A (groupRequired: true), B (groupRequired: true), C (groupRequired: false).
2. Render with MarketingForm and check only checkbox C.
3. Submit the form. Validation fails with 'Please select at least one option' even though an option in the group is selected.
Fixing Code Block
// In handleSubmit, replace the existing group validation and individual required validation with:
const groupedFields = fields.filter(f => f.group);
const groupMap = new Map<string, { fields: typeof groupedFields; groupRequired: boolean }>();
groupedFields.forEach(f => {
const groupName = f.group!;
const entry = groupMap.get(groupName) || { fields: [], groupRequired: false };
entry.fields.push(f);
if (f.groupRequired) entry.groupRequired = true;
groupMap.set(groupName, entry);
});
// Validate groups: if any member has groupRequired, at least one checkbox in the group must be checked
for (const [groupName, entry] of groupMap) {
if (entry.groupRequired) {
const anyChecked = entry.fields.some(f => formState[f.name]);
if (!anyChecked) {
const firstField = entry.fields[0];
errors[firstField.name] = 'Please select at least one option';
}
}
}
// Validate individual required fields (including grouped ones)
const requiredFields = fields.filter(f => f.required);
requiredFields.forEach(f => {
if (!formState[f.name]) {
errors[f.name] = 'This field is required';
}
});
The group map is now built from every checkbox that has a group, while still tracking whether any member sets groupRequired. Group validation checks for at least one checked checkbox in the group whenever groupRequired is true on any member. The individual required filter no longer excludes grouped checkboxes, so a checkbox with both required and group will be individually enforced as documented.
Edge Case Audit
This fix may change behavior for schemas that relied on the old (incorrect) group logic where only groupRequired: true checkboxes were required. However, the only current usage sets groupRequired on every member, so no live form is affected. If a schema intentionally mixed flags and expected the old behavior, it will now accept selections that were previously rejected, which is the documented contract. The removal of `!f.group` from individual required may enforce required on previously grouped checkboxes that were silently skipped. Rollback: if unexpected regressions occur, revert to the previous validation code or add a schema validation rule to forbid the combination of `required` and `group` until the schema can be updated.