(app App, collection *Collection)
| 249 | } |
| 250 | |
| 251 | func (f *RelationField) checkCollectionId(app App, collection *Collection) validation.RuleFunc { |
| 252 | return func(value any) error { |
| 253 | v, _ := value.(string) |
| 254 | if v == "" { |
| 255 | return nil // nothing to check |
| 256 | } |
| 257 | |
| 258 | var oldCollection *Collection |
| 259 | |
| 260 | if !collection.IsNew() { |
| 261 | var err error |
| 262 | oldCollection, err = app.FindCachedCollectionByNameOrId(collection.Id) |
| 263 | if err != nil { |
| 264 | return err |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | // prevent collectionId change |
| 269 | if oldCollection != nil { |
| 270 | oldField, _ := oldCollection.Fields.GetById(f.Id).(*RelationField) |
| 271 | if oldField != nil && oldField.CollectionId != v { |
| 272 | return validation.NewError( |
| 273 | "validation_field_relation_change", |
| 274 | "The relation collection cannot be changed.", |
| 275 | ) |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | relCollection, _ := app.FindCachedCollectionByNameOrId(v) |
| 280 | |
| 281 | // validate collectionId |
| 282 | if relCollection == nil || relCollection.Id != v { |
| 283 | return validation.NewError( |
| 284 | "validation_field_relation_missing_collection", |
| 285 | "The relation collection doesn't exist.", |
| 286 | ) |
| 287 | } |
| 288 | |
| 289 | // allow only views to have relations to other views |
| 290 | // (see https://github.com/pocketbase/pocketbase/issues/3000) |
| 291 | if !collection.IsView() && relCollection.IsView() { |
| 292 | return validation.NewError( |
| 293 | "validation_relation_field_non_view_base_collection", |
| 294 | "Only view collections are allowed to have relations to other views.", |
| 295 | ) |
| 296 | } |
| 297 | |
| 298 | return nil |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | // --- |
| 303 |
no test coverage detected