Skip to content

Commit

Permalink
Fixes for #7252 - custom fields not validating / no validaton message…
Browse files Browse the repository at this point in the history
…s in API w/form requests
  • Loading branch information
snipe committed Jul 18, 2019
1 parent cef22c3 commit ff7e207
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
11 changes: 1 addition & 10 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ public function render($request, Exception $e)
return response()->json(Helper::formatStandardApiResponse('error', null, $className . ' not found'), 200);
}

if ($e instanceof \Illuminate\Validation\ValidationException) {
return response()->json(Helper::formatStandardApiResponse('error', null, $e->response['messages'], 400));
}

if ($this->isHttpException($e)) {

$statusCode = $e->getStatusCode();
Expand All @@ -85,11 +81,6 @@ public function render($request, Exception $e)

}
}
// Try to parse 500 Errors in a bit nicer way when debug is enabled.
if (config('app.debug')) {
return response()->json(Helper::formatStandardApiResponse('error', null, "An Error has occured! " . $e->getMessage()), 500);
}

}


Expand Down Expand Up @@ -128,6 +119,6 @@ protected function unauthenticated($request, AuthenticationException $exception)
*/
protected function invalidJson($request, ValidationException $exception)
{
return response()->json($exception->errors(), $exception->status);
return response()->json(Helper::formatStandardApiResponse('error', null, $exception->errors(), 400));
}
}
29 changes: 26 additions & 3 deletions app/Http/Requests/SaveUserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace App\Http\Requests;

use App\Http\Requests\Request;
use App\Models\Setting;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Contracts\Validation\Validator;

class SaveUserRequest extends Request
class SaveUserRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
Expand Down Expand Up @@ -33,6 +35,7 @@ public function rules()
// Brand new user
case 'POST':
{
\Log::debug('New user');
$rules['first_name'] = 'required|string|min:1';
$rules['username'] = 'required_unless:ldap_import,1|string|min:1';
if ($this->request->get('ldap_import') == false)
Expand All @@ -58,8 +61,28 @@ public function rules()

default:break;
}


\Log::debug($rules);
return $rules;

}

// /**
// * Handle a failed validation attempt.
// *
// * public function json($data = [], $status = 200, array $headers = [], $options = 0)
// *
// * @param \Illuminate\Contracts\Validation\Validator $validator
// * @return void
// *
// * @throws \Illuminate\Http\Exceptions\HttpResponseException
// */
// protected function failedValidation(Validator $validator)
// {
//
// throw new HttpResponseException(response()->json([
// 'message' => 'The given data is invalid',
// 'errors' => $validator->errors()
// ], 422));
// }
}

0 comments on commit ff7e207

Please sign in to comment.