Skip to content

Commit

Permalink
Fixes snipe#7252 form request changes (snipe#7272)
Browse files Browse the repository at this point in the history
* Fixes for snipe#7252 - custom fields not validating / no validaton messages in API w/form requests

* Removed debug info

* More fixes for snipe#7252

This is mostly working as intended, if not yet the way Laravel wants us to do it.

Right now, the API returns correctly, and the form UI will return highlighted errors, with the input filled in ~sometimes~. I’m not sure why it’s only sometimes yet, but this is potentially progress.

* Removed experimental method

* Check for digits_between:0,240 for warranty

* Removed debug code
  • Loading branch information
snipe authored Jul 18, 2019
1 parent eec445f commit 55ee90b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 28 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));
}
}
27 changes: 12 additions & 15 deletions app/Http/Requests/AssetRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use App\Models\AssetModel;
use Session;

use Illuminate\Http\Exceptions\HttpResponseException;

use Illuminate\Contracts\Validation\Validator;

Expand Down Expand Up @@ -32,7 +32,7 @@ public function rules()
'model_id' => 'required|integer|exists:models,id',
'status_id' => 'required|integer|exists:status_labels,id',
'company_id' => 'integer|nullable',
'warranty_months' => 'numeric|nullable',
'warranty_months' => 'numeric|nullable|digits_between:0,240',
'physical' => 'integer|nullable',
'checkout_date' => 'date',
'checkin_date' => 'date',
Expand All @@ -48,7 +48,7 @@ public function rules()

$rules['asset_tag'] = ($settings->auto_increment_assets == '1') ? 'max:255' : 'required';

if($this->request->get('model_id') != '') {
if ($this->request->get('model_id') != '') {
$model = AssetModel::find($this->request->get('model_id'));

if (($model) && ($model->fieldset)) {
Expand All @@ -60,13 +60,6 @@ public function rules()

}

public function response(array $errors)
{
$this->session()->flash('errors', Session::get('errors', new \Illuminate\Support\ViewErrorBag)
->put('default', new \Illuminate\Support\MessageBag($errors)));
\Input::flash();
return parent::response($errors);
}

/**
* Handle a failed validation attempt.
Expand All @@ -76,13 +69,17 @@ public function response(array $errors)
* @param \Illuminate\Contracts\Validation\Validator $validator
* @return void
*
* @throws \Illuminate\Validation\ValidationException
* @throws \Illuminate\Http\Exceptions\HttpResponseException
*/
protected function failedValidation(Validator $validator)
{
return response()->json([
'message' => 'The given data is invalid',
'errors' => $validator->errors()
], 422);
$this->session()->flash('errors', Session::get('errors', new \Illuminate\Support\ViewErrorBag)
->put('default', new \Illuminate\Support\MessageBag($validator->errors()->toArray())));
\Input::flash();
throw new HttpResponseException(response()->json([
'status' => 'error',
'messages' => $validator->errors(),
'payload' => null
], 422));
}
}
7 changes: 5 additions & 2 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 @@ -62,4 +64,5 @@ public function rules()
return $rules;

}

}
2 changes: 1 addition & 1 deletion app/Models/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Asset extends Depreciable
'model_id' => 'required|integer|exists:models,id',
'status_id' => 'required|integer|exists:status_labels,id',
'company_id' => 'integer|nullable',
'warranty_months' => 'numeric|nullable|max:240',
'warranty_months' => 'numeric|nullable|digits_between:0,240',
'physical' => 'numeric|max:1|nullable',
'checkout_date' => 'date|max:10|min:10|nullable',
'checkin_date' => 'date|max:10|min:10|nullable',
Expand Down

0 comments on commit 55ee90b

Please sign in to comment.