snipe-it/app/Http/Requests/AssetRequest.php
snipe bb42109c0c
Added a clarifying comment
Signed-off-by: snipe <snipe@snipe.net>
2020-05-11 18:10:45 -07:00

88 lines
2.9 KiB
PHP

<?php
namespace App\Http\Requests;
use App\Models\AssetModel;
use Session;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Contracts\Validation\Validator;
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()
{
// Below, asset_tag is duplicated here and in the model, largely because
// the functional tests will fail because of the way Form Requests work in
// Laravel.
$rules = [
'asset_tag' => 'required|min:1|max:255|unique_undeleted',
'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|digits_between:0,240',
'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',
];
$settings = \App\Models\Setting::getSettings();
if ($this->request->get('model_id') != '') {
$model = AssetModel::find($this->request->get('model_id'));
if (($model) && (isset($model->fieldset)) && ($model->fieldset)) {
$rules += $model->fieldset->validation_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)
{
$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));
}
}