fix(input/choice): add specific validation func
This commit is contained in:
parent
93b08069d1
commit
dc6db954ed
2 changed files with 29 additions and 0 deletions
|
|
@ -1,5 +1,11 @@
|
|||
## [Unreleased]
|
||||
|
||||
## v1.1.5
|
||||
|
||||
### Fixed
|
||||
|
||||
- fix(input/choice): add specific validation func
|
||||
|
||||
## v1.1.4
|
||||
|
||||
### Fixed
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue