Skip to content

Commit

Permalink
fix bug validate
Browse files Browse the repository at this point in the history
  • Loading branch information
YaangVu committed Dec 10, 2023
1 parent 94e53f3 commit 4da07ec
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
7 changes: 6 additions & 1 deletion src/Base/Utility/Validatable.php
Expand Up @@ -23,6 +23,7 @@ trait Validatable
* @param bool $throwable
*
* @return bool|array
* @throws BadRequestException
*/
public function validateStoreRequest(object $request, array $rules = [], array $messages = [],
array $customAttributes = [], bool $throwable = true): bool|array
Expand All @@ -41,6 +42,7 @@ public function validateStoreRequest(object $request, array $rules = [], array $
* @param bool $throwable
*
* @return bool|array
* @throws BadRequestException
*/
public static function doValidate(object $request, array $rules = [], array $messages = [],
array $customAttributes = [], bool $throwable = true): bool|array
Expand All @@ -57,7 +59,8 @@ public static function doValidate(object $request, array $rules = [], array $mes
return true;

if ($throwable)
throw new BadRequestException(['messages' => $validator->errors()->toArray()]);
throw new
BadRequestException(__('laravel-base.request-not-valid'), messages: $validator->errors()->toArray());
else
return $validator->errors()->toArray();
}
Expand All @@ -73,6 +76,7 @@ public static function doValidate(object $request, array $rules = [], array $mes
* @param bool $throwable
*
* @return bool|array
* @throws BadRequestException
*/
public function validateUpdateRequest(int|string $id, object $request, array $rules = [], array $messages = [],
array $customAttributes = [], bool $throwable = true): bool|array
Expand All @@ -94,6 +98,7 @@ public function validateUpdateRequest(int|string $id, object $request, array $ru
* @param bool $throwable
*
* @return bool|array
* @throws BadRequestException
*/
public function validatePutUpdateRequest(int|string $id, object $request, array $rules = [], array $messages = [],
array $customAttributes = [], bool $throwable = true): bool|array
Expand Down
29 changes: 21 additions & 8 deletions src/Exception/BaseException.php
Expand Up @@ -12,28 +12,41 @@ class BaseException extends Exception
{
public int $statusCode = Response::HTTP_INTERNAL_SERVER_ERROR;

public function __construct(string $message = "", int $code = 0,
?Throwable $previous = null, private readonly string $error = '')
public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null,
private readonly string $error = '', private readonly array $messages = [])
{
parent::__construct($message, $code, $previous);
}

public function render($request): JsonResponse
{
$response = [
'message' => $this->getMessage(),
'error' => $this->getError(),
'code' => $this->getCode(),
'file' => $this->getFile(),
'line' => $this->getLine(),
'trace' => $this->getTrace(),
'message' => $this->getMessage(),
'messages' => $this->getMessages(),
'error' => $this->getError(),
'code' => $this->getCode(),
'file' => $this->getFile(),
'line' => $this->getLine(),
'trace' => $this->getTrace(),
];

Log::error($this->getMessage(), $response);

// if the application was not enabling debug mode, then return only a message
if (!config('app.debug'))
$response = [
'message' => $this->getMessage(),
'messages' => $this->getMessages(),
];

return response()->json($response)->setStatusCode($this->statusCode);
}

public function getMessages(): array
{
return $this->messages;
}

public function getError(): string
{
return $this->error;
Expand Down
13 changes: 7 additions & 6 deletions src/lang/en/laravel-base.php
Expand Up @@ -5,10 +5,11 @@
*/

return [
'server-error' => 'Internal Server Error',
'can-not-del' => 'The :attribute can not be deleted',
'can-not-add' => 'The :attribute can not be added',
'can-not-update' => 'The :attribute can not be updated',
'not-found' => 'The :attribute can not be found',
'query-error' => 'Query Error'
'server-error' => 'Internal Server Error',
'can-not-del' => 'The :attribute can not be deleted',
'can-not-add' => 'The :attribute can not be added',
'can-not-update' => 'The :attribute can not be updated',
'not-found' => 'The :attribute can not be found',
'query-error' => 'Query Error',
'request-not-valid' => 'The request is not valid'
];
13 changes: 7 additions & 6 deletions src/lang/vi/laravel-base.php
Expand Up @@ -5,10 +5,11 @@
*/

return [
'server-error' => 'Hệ thống đang gặp lỗi',
'can-not-del' => 'Đối tượng :attribute không thể xóa',
'can-not-add' => 'Đối tượng :attribute không thể thêm',
'can-not-update' => 'Đối tượng :attribute không thể chỉnh sửa',
'not-found' => 'Đối tượng :attribute không tìm thấy',
'query-error' => 'Câu lệnh lỗi',
'server-error' => 'Hệ thống đang gặp lỗi',
'can-not-del' => 'Đối tượng :attribute không thể xóa',
'can-not-add' => 'Đối tượng :attribute không thể thêm',
'can-not-update' => 'Đối tượng :attribute không thể chỉnh sửa',
'not-found' => 'Đối tượng :attribute không tìm thấy',
'query-error' => 'Câu lệnh lỗi',
'request-not-valid' => 'Dữ liệu không hợp lệ'
];

0 comments on commit 4da07ec

Please sign in to comment.