Skip to content

Commit

Permalink
Merge pull request #7 from fabianmieller/master
Browse files Browse the repository at this point in the history
✨ added validation customAttributes
  • Loading branch information
acidjazz authored Feb 15, 2023
2 parents 6dcdcf7 + 89adbe8 commit 52c544d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
42 changes: 40 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ Add the trait
use acidjazz\metapi\MetApi;
class Controller
{
use Metapi;
use Metapi;
```

## Examples
## Examples

```php
<?php
Expand Down Expand Up @@ -112,3 +112,41 @@ bob({
{
```

**Add [custom attributes](https://laravel.com/docs/9.x/validation#specifying-custom-attribute-values) to validation.**

```php
public function send(Request $request)
{
$this->option('contact.email', 'required|email', [], 'Email Address')
->option('contact.name', 'required|string', [], 'Firstname')
->option('contact.surname', 'required|string', [], 'Lastname')
->verify();
...
$this->render($results);
}
```

`POST /send`

```json
{
"status": "error",
"errors": [
{
"status": 400,
"message": "contact.email",
"detail": "Email Address is a required field."
},
{
"status": 400,
"message": "contact.name",
"detail": "Firstname is a required field."
},
{
"status": 400,
"message": "contact.surname",
"detail": "Lastname is a required field."
}
]
}
```
12 changes: 10 additions & 2 deletions src/MetApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ public function metApiInit(Request $request)
* @param string $name
* @param array|string $rules
* @param array $messages<string, string>
* @param string $customAttribute
* @return Controller
*/
public function option(string $name, array|string $rules, array $messages = []): self
public function option(string $name, array|string $rules, array $messages = [], string $customAttribute = null): self
{
$this->query['options']['rules'][$name] = $rules;

Expand All @@ -68,6 +69,13 @@ public function option(string $name, array|string $rules, array $messages = []):
);
}

if ($customAttribute) {
if (!array_key_exists('customAttributes', $this->query['options'])) {
$this->query['options']['customAttributes'] = [];
}
$this->query['options']['customAttributes'][$name] = $customAttribute;
}

return $this;
}

Expand Down Expand Up @@ -150,7 +158,7 @@ public function paginate($collection, $perpage = 50, $maxPages = 10)
public function verify($abort = true)
{

$validate = Validator::make($this->request->all(), $this->query['options']['rules'], $this->query['options']['messages'] ?? []);
$validate = Validator::make($this->request->all(), $this->query['options']['rules'], $this->query['options']['messages'] ?? [], $this->query['options']['customAttributes'] ?? []);

if ($validate->fails()) {
foreach ($validate->errors()->toArray() as $key => $value) {
Expand Down

0 comments on commit 52c544d

Please # to comment.