Skip to content

Commit 7d492f3

Browse files
committed
new featured added
1 parent 80b0cc8 commit 7d492f3

File tree

1 file changed

+51
-10
lines changed

1 file changed

+51
-10
lines changed

README.md

+51-10
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function add()
4343
$user = $this->Users->patchEntity($user, $this->request->getData());
4444
if (!$this->Users->save($user)) {
4545
throw new JsonApiException($user, 'Save failed');
46-
// throw new JsonApiException($user, 'Save failed', 418); // you set the response's status code in the 3rd parameter
46+
// throw new JsonApiException($user, 'Save failed', 418); // you can set the response's status code in the 3rd parameter
4747
}
4848
}
4949
$this->set(compact('user'));
@@ -55,14 +55,55 @@ If the save failed you will get a response like this.
5555

5656
```json
5757
{
58-
"message": "Save failed",
59-
"url": "/users.json",
60-
"line": 12,
61-
"errorCount": 1,
62-
"errors": {
63-
"password": {
64-
"_required": "This field is required"
65-
}
66-
}
58+
"message": "Save failed",
59+
"url": "/users.json",
60+
"line": 12,
61+
"errorCount": 1,
62+
"errors": {
63+
"password": {
64+
"_required": "This field is required"
65+
}
66+
}
67+
}
68+
```
69+
70+
You can us it with an array of entities also.
71+
72+
```php
73+
// for example in /src/Controller/UsersController.php slightly change the baked add function
74+
use JsonApiException\Error\Exception\JsonApiException;
75+
76+
public function bulkAdd()
77+
{
78+
$users = $this->Users->newEntities($this->request->getData());
79+
if (!$this->Users->saveMany($users)) {
80+
throw new JsonApiException($users, 'Errors in request data');
81+
}
82+
$this->set(compact('users'));
83+
$this->viewBuilder()->setOption('serialize', ['users']);
84+
}
85+
```
86+
87+
If the save failed you will get a response like this.
88+
As at this point we do not have an `id` for the entity, the error messages can not contain it. So an empty array will be present in the `errors` array if an entity does not have any errors. So the number of the entity will match the number of the error entries, entity #3's error message will be the third element of the error array.
89+
90+
```json
91+
{
92+
"message": "Errors in request data",
93+
"url": "/users.json",
94+
"line": 12,
95+
"errorCount": 2,
96+
"errors": [
97+
{
98+
"name": {
99+
"_empty": "name is required"
100+
}
101+
},
102+
{
103+
"name": {
104+
"_empty": "name is required"
105+
}
106+
}
107+
]
67108
}
68109
```

0 commit comments

Comments
 (0)