ValidateValue implements [Field.ValidateValue] interface method.
(ctx context.Context, app App, record *Record)
| 196 | |
| 197 | // ValidateValue implements [Field.ValidateValue] interface method. |
| 198 | func (f *RelationField) ValidateValue(ctx context.Context, app App, record *Record) error { |
| 199 | ids := list.ToUniqueStringSlice(record.GetRaw(f.Name)) |
| 200 | if len(ids) == 0 { |
| 201 | if f.Required { |
| 202 | return validation.ErrRequired |
| 203 | } |
| 204 | return nil // nothing to check |
| 205 | } |
| 206 | |
| 207 | if f.MinSelect > 0 && len(ids) < f.MinSelect { |
| 208 | return validation.NewError("validation_not_enough_values", "Select at least {{.minSelect}}"). |
| 209 | SetParams(map[string]any{"minSelect": f.MinSelect}) |
| 210 | } |
| 211 | |
| 212 | maxSelect := max(f.MaxSelect, 1) |
| 213 | if len(ids) > maxSelect { |
| 214 | return validation.NewError("validation_too_many_values", "Select no more than {{.maxSelect}}"). |
| 215 | SetParams(map[string]any{"maxSelect": maxSelect}) |
| 216 | } |
| 217 | |
| 218 | // check if the related records exist |
| 219 | // --- |
| 220 | relCollection, err := app.FindCachedCollectionByNameOrId(f.CollectionId) |
| 221 | if err != nil { |
| 222 | return validation.NewError("validation_missing_rel_collection", "Relation connection is missing or cannot be accessed") |
| 223 | } |
| 224 | |
| 225 | var total int |
| 226 | _ = app.ConcurrentDB(). |
| 227 | Select("count(*)"). |
| 228 | From(relCollection.Name). |
| 229 | AndWhere(dbx.In("id", list.ToInterfaceSlice(ids)...)). |
| 230 | Row(&total) |
| 231 | if total != len(ids) { |
| 232 | return validation.NewError("validation_missing_rel_records", "Failed to find all relation records with the provided ids") |
| 233 | } |
| 234 | // --- |
| 235 | |
| 236 | return nil |
| 237 | } |
| 238 | |
| 239 | // ValidateSettings implements [Field.ValidateSettings] interface method. |
| 240 | func (f *RelationField) ValidateSettings(ctx context.Context, app App, collection *Collection) error { |
nothing calls this directly
no test coverage detected