2016-03-25 01:18:05 -07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
|
2016-03-25 19:26:22 -07:00
|
|
|
use App\Models\AssetModel;
|
2016-05-20 17:23:34 -07:00
|
|
|
use Session;
|
2019-07-18 14:32:23 -07:00
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
2019-05-24 04:47:35 -07:00
|
|
|
|
2019-05-24 03:55:31 -07:00
|
|
|
use Illuminate\Contracts\Validation\Validator;
|
2016-06-22 12:27:41 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
class AssetRequest extends Request
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Determine if the user is authorized to make this request.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function authorize()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the validation rules that apply to the request.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function rules()
|
|
|
|
{
|
|
|
|
$rules = [
|
2017-01-12 02:19:36 -08:00
|
|
|
'name' => 'max:255|nullable',
|
2017-01-12 02:41:02 -08:00
|
|
|
'model_id' => 'required|integer|exists:models,id',
|
|
|
|
'status_id' => 'required|integer|exists:status_labels,id',
|
2016-12-29 08:10:52 -08:00
|
|
|
'company_id' => 'integer|nullable',
|
2019-07-18 14:32:23 -07:00
|
|
|
'warranty_months' => 'numeric|nullable|digits_between:0,240',
|
2016-12-29 08:10:52 -08:00
|
|
|
'physical' => 'integer|nullable',
|
|
|
|
'checkout_date' => 'date',
|
|
|
|
'checkin_date' => 'date',
|
|
|
|
'supplier_id' => 'integer|nullable',
|
|
|
|
'status' => 'integer|nullable',
|
|
|
|
'purchase_cost' => 'numeric|nullable',
|
2016-12-29 22:23:36 -08:00
|
|
|
"assigned_user" => 'sometimes:required_without_all:assigned_asset,assigned_location',
|
|
|
|
"assigned_asset" => 'sometimes:required_without_all:assigned_user,assigned_location',
|
|
|
|
"assigned_location" => 'sometimes:required_without_all:assigned_user,assigned_asset',
|
2016-03-25 01:18:05 -07:00
|
|
|
];
|
|
|
|
|
2018-01-03 17:24:32 -08:00
|
|
|
$settings = \App\Models\Setting::getSettings();
|
2020-05-11 18:10:45 -07:00
|
|
|
|
2020-05-14 00:55:47 -07:00
|
|
|
$rules['asset_tag'] = ($settings->auto_increment_assets == '1') ? 'max:255' : 'required';
|
|
|
|
|
2019-07-18 14:32:23 -07:00
|
|
|
if ($this->request->get('model_id') != '') {
|
2018-07-12 18:24:43 -07:00
|
|
|
$model = AssetModel::find($this->request->get('model_id'));
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2020-04-09 11:18:54 -07:00
|
|
|
if (($model) && (isset($model->fieldset)) && ($model->fieldset)) {
|
2018-07-12 18:24:43 -07:00
|
|
|
$rules += $model->fieldset->validation_rules();
|
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return $rules;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-24 01:12:21 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a failed validation attempt.
|
|
|
|
*
|
2019-05-24 03:55:31 -07:00
|
|
|
* public function json($data = [], $status = 200, array $headers = [], $options = 0)
|
|
|
|
*
|
2019-05-24 01:12:21 -07:00
|
|
|
* @param \Illuminate\Contracts\Validation\Validator $validator
|
|
|
|
* @return void
|
|
|
|
*
|
2019-07-18 14:32:23 -07:00
|
|
|
* @throws \Illuminate\Http\Exceptions\HttpResponseException
|
2019-05-24 01:12:21 -07:00
|
|
|
*/
|
|
|
|
protected function failedValidation(Validator $validator)
|
|
|
|
{
|
2019-07-18 14:32:23 -07:00
|
|
|
$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));
|
2019-05-24 01:12:21 -07:00
|
|
|
}
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|