fix(input/choice): add specific validation func

This commit is contained in:
Simon Vieille 2025-07-29 13:45:36 +02:00
commit dc6db954ed
Signed by: deblan
GPG key ID: 579388D585F70417
2 changed files with 29 additions and 0 deletions

View file

@ -1,5 +1,11 @@
## [Unreleased]
## v1.1.5
### Fixed
- fix(input/choice): add specific validation func
## v1.1.4
### Fixed

View file

@ -19,6 +19,7 @@ import (
"reflect"
"github.com/spf13/cast"
"gitnet.fr/deblan/go-form/validation"
)
type Choice struct {
@ -124,6 +125,28 @@ func NewFieldChoice(name string) *Field {
NewOption("empty_choice_label", "None"),
)
f.Validate = func(field *Field) bool {
isValid := FieldValidation(field)
if len(validation.NewNotBlank().Validate(field.Data)) == 0 {
choices := field.GetOption("choices").Value.(*Choices)
isValidChoice := true
for _, choice := range choices.GetChoices() {
if !choices.Match(field, choice.Value) {
isValidChoice = false
}
}
if !isValidChoice {
field.Errors = append(field.Errors, validation.Error("This value is not valid."))
isValid = false
}
}
return isValid
}
f.WithBeforeBind(func(data any) (any, error) {
choices := f.GetOption("choices").Value.(*Choices)