package fieldgroups import ( "fmt" "pdf-wizard/internal/models" ) // ConditionalHandler handles groups where a checkbox (typically "Other") is // paired with one or more text fields. The text fields are required when the // checkbox is selected. type ConditionalHandler struct{} func init() { Register(ConditionalHandler{}) } func (ConditionalHandler) Type() string { return "conditional_other" } func (ConditionalHandler) Validate(fields []models.FormField, fieldValues map[int]string) error { var checkboxChecked bool var textFields []models.FormField for _, f := range fields { if f.Type == "button" { if v, ok := fieldValues[f.ID]; ok && v == "1" { checkboxChecked = true } } else { textFields = append(textFields, f) } } if !checkboxChecked { return nil } // Text fields are required when checkbox is checked for _, f := range textFields { if v, ok := fieldValues[f.ID]; !ok || v == "" { return fmt.Errorf("field '%s' is required when 'Other' is selected", f.FieldName) } } return nil } func (ConditionalHandler) ConvertToFillValues(fields []models.FormField, fieldValues map[int]string) map[int]string { result := make(map[int]string) for _, f := range fields { if v, ok := fieldValues[f.ID]; ok { result[f.ID] = v } else { result[f.ID] = "" } } return result }