Creating a Task and Linking It to a Project
Creating a Task and Linking It to a Project
~12 Min. Lesezeit Zuletzt aktualisiert am August 8, 2026
Chapter 37 showed the basic case – this chapter covers ERROR cases when linking and shows the path via the nested endpoint from chapter 39.
Linking with an invalid IRI
curl -k -X POST https://localhost/api/tasks \
-H 'Content-Type: application/json' \
-d '{"title": "Test", "project": "/api/projects/99999"}'{
"violations": [
{"propertyPath": "project", "message": "Item not found for \"/api/projects/99999\"."}
]
}Status 422 – API Platform RESOLVES the IRI BEFORE saving, and reports a VALIDATION error if the target does NOT exist, in the EXACT same violations format from chapter 20.
Linking with the wrong resource type
curl -k -X POST https://localhost/api/tasks \
-H 'Content-Type: application/json' \
-d '{"title": "Test", "project": "/api/tags/1"}'ALSO 422 – /api/tags/1 is a SYNTACTICALLY valid IRI, but of the WRONG type (Tag instead of Project). API Platform CHECKS the target type, not just its existence.
Creating via the nested endpoint
The sub-resource path from chapter 39 (/api/projects/{projectId}/tasks) only had GetCollection in our example – for POST via this path, the operations list would need to be EXTENDED with new Post():
operations: [new GetCollection(), new Post()],curl -k -X POST https://localhost/api/projects/1/tasks \
-H 'Content-Type: application/json' \
-d '{"title": "Prepare deployment"}'WITHOUT an explicit project field in the body – API Platform sets it AUTOMATICALLY from the URL (projectId), since Link (chapter 39) ALREADY knows this connection.
Achtung: This variant is NOT permanently adopted for OUR project (the flat POST /api/tasks with an explicit project field stays the PRIMARY path) – shown as an ALTERNATIVE that can make more sense depending on frontend design.
Tipp: In React Hook Form (block 9), the violations error for propertyPath: "project" can be handled JUST LIKE any other field error – from the API's perspective, a failed relationship resolution is JUST ANOTHER validation error.