diff --git a/readme.md b/readme.md index 3b55e7e..6e32c69 100644 --- a/readme.md +++ b/readme.md @@ -40,10 +40,10 @@ Add the trait use acidjazz\metapi\MetApi; class Controller { - use Metapi; + use Metapi; ``` -## Examples +## Examples ```php 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." + } + ] +} +``` diff --git a/src/MetApi.php b/src/MetApi.php index a9c3994..2350dbb 100644 --- a/src/MetApi.php +++ b/src/MetApi.php @@ -49,9 +49,10 @@ public function metApiInit(Request $request) * @param string $name * @param array|string $rules * @param array $messages + * @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; @@ -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; } @@ -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) {