snipe-it/app/Http/Requests/AssetRequest.php

89 lines
2.6 KiB
PHP
Raw Normal View History

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;
use Session;
2019-05-24 04:47:35 -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',
'model_id' => 'required|integer|exists:models,id',
'status_id' => 'required|integer|exists:status_labels,id',
'company_id' => 'integer|nullable',
'warranty_months' => 'numeric|nullable',
'physical' => 'integer|nullable',
'checkout_date' => 'date',
'checkin_date' => 'date',
'supplier_id' => 'integer|nullable',
'status' => 'integer|nullable',
'purchase_cost' => 'numeric|nullable',
"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
];
$settings = \App\Models\Setting::getSettings();
$rules['asset_tag'] = ($settings->auto_increment_assets == '1') ? 'max:255' : 'required';
if($this->request->get('model_id') != '') {
$model = AssetModel::find($this->request->get('model_id'));
2016-03-25 01:18:05 -07:00
if (($model) && ($model->fieldset)) {
$rules += $model->fieldset->validation_rules();
}
2016-03-25 01:18:05 -07:00
}
return $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);
2016-03-25 01:18:05 -07:00
}
2019-05-24 01:12:21 -07:00
/**
* Handle a failed validation attempt.
*
* 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
*
* @throws \Illuminate\Validation\ValidationException
*/
protected function failedValidation(Validator $validator)
{
2019-05-24 04:47:35 -07:00
return response()->json([
'message' => 'The given data is invalid',
'errors' => $validator->errors()
], 422);
2019-05-24 01:12:21 -07:00
}
2016-03-25 01:18:05 -07:00
}