From 5be5e3271d4837d59f214c8c17302a986aafca10 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 24 May 2019 03:55:31 -0700 Subject: [PATCH] Trying to fix ajax asset validation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This I think gets us closer, but still not handling the validation on the asset properly. When I do a print_r of the validation in the other items, its looking for an error bag that looks something like this: ``` Illuminate\Support\MessageBag Object ( [messages:protected] => Array ( [name] => Array ( [0] => The name field is required. ) [seats] => Array ( [0] => The seats field is required. ) [category_id] => Array ( [0] => The category id field is required. ) ) [format:protected] => :message ) ``` Currently the Assets ajax returns: ``` [2019-05-24 06:52:06] develop.ERROR: array ( 'messages' => array ( 'model_id' => array ( 0 => 'The model id field is required.', ), 'status_id' => array ( 0 => 'The status id field is required.', ), 'asset_tag' => array ( 0 => 'The asset tag field is required.', ), ), ) ``` So not sure why it’s not working. --- app/Http/Requests/AssetRequest.php | 34 ++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/app/Http/Requests/AssetRequest.php b/app/Http/Requests/AssetRequest.php index cf940b70c9..d16bf863e4 100644 --- a/app/Http/Requests/AssetRequest.php +++ b/app/Http/Requests/AssetRequest.php @@ -6,6 +6,8 @@ use App\Models\AssetModel; use Session; use Illuminate\Http\Exceptions\HttpResponseException; use App\Helpers\Helper; +use App\Http\Requests\Request; +use Illuminate\Contracts\Validation\Validator; class AssetRequest extends Request { @@ -70,6 +72,8 @@ class AssetRequest extends Request /** * Handle a failed validation attempt. * + * public function json($data = [], $status = 200, array $headers = [], $options = 0) + * * @param \Illuminate\Contracts\Validation\Validator $validator * @return void * @@ -77,7 +81,33 @@ class AssetRequest extends Request */ protected function failedValidation(Validator $validator) { - \Log::error('failedValidation'); - throw new HttpResponseException(response()->json($validator->errors(), 'error', 422)); + // $status, $payload = null, $messages = null + //$array['status'] = 'error'; + //$array['payload'] = null; + $array['messages'] = []; + //$array['errors'] = []; + + if ($validator->errors()) { + foreach ($validator->errors()->messages() as $field => $errors) { + $array['messages'][$field] = $errors; + //$array['errors'][$field] = $errors; + + } + } + + +// $errors = $validator->errors(); +// \Log::debug('failedValidation'); +// \Log::debug('validator->errors()->all()'); +// \Log::debug(print_r($validator->errors()->all(), true)); +// \Log::debug('validator->errors()'); +// \Log::debug(print_r($validator->errors(), true)); +// \Log::debug('validator->errors()->messages()'); +// \Log::debug(print_r($validator->errors()->messages(), true)); +// \Log::debug('array'); + \Log::error($array); + + throw new HttpResponseException(response()->json($array, 200)); + } }