ValidateValue implements [Field.ValidateValue] interface method.
(ctx context.Context, app App, record *Record)
| 183 | |
| 184 | // ValidateValue implements [Field.ValidateValue] interface method. |
| 185 | func (f *SelectField) ValidateValue(ctx context.Context, app App, record *Record) error { |
| 186 | normalizedVal := list.ToUniqueStringSlice(record.GetRaw(f.Name)) |
| 187 | if len(normalizedVal) == 0 { |
| 188 | if f.Required { |
| 189 | return validation.ErrRequired |
| 190 | } |
| 191 | return nil // nothing to check |
| 192 | } |
| 193 | |
| 194 | maxSelect := max(f.MaxSelect, 1) |
| 195 | |
| 196 | // check max selected items |
| 197 | if len(normalizedVal) > maxSelect { |
| 198 | return validation.NewError("validation_too_many_values", "Select no more than {{.maxSelect}}"). |
| 199 | SetParams(map[string]any{"maxSelect": maxSelect}) |
| 200 | } |
| 201 | |
| 202 | // check against the allowed values |
| 203 | for _, val := range normalizedVal { |
| 204 | if !slices.Contains(f.Values, val) { |
| 205 | return validation.NewError("validation_invalid_value", "Invalid value {{.value}}"). |
| 206 | SetParams(map[string]any{"value": val}) |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return nil |
| 211 | } |
| 212 | |
| 213 | // ValidateSettings implements [Field.ValidateSettings] interface method. |
| 214 | func (f *SelectField) ValidateSettings(ctx context.Context, app App, collection *Collection) error { |
nothing calls this directly
no test coverage detected