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

59 lines
1.6 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Requests;
use App\Models\Asset;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\Rule;
class UpdateAssetRequest extends ImageUploadRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return Gate::allows('update', new Asset);
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$rules = array_merge(
parent::rules(),
(new Asset)->getRules(),
// this is to overwrite rulesets that include required
[
'model_id' => ['integer', 'exists:models,id,deleted_at,NULL', 'not_array'],
'status_id' => ['integer', 'exists:status_labels,id'],
'asset_tag' => [
'min:1', 'max:255', 'not_array',
Rule::unique('assets', 'asset_tag')->ignore($this->asset)->withoutTrashed()
],
],
2024-03-21 07:26:45 -07:00
);
2024-04-23 15:35:33 -07:00
// OR
$rules2 = array_merge(
parent::rules(),
2024-04-23 15:38:40 -07:00
// collects rules, 'rejects' required rules
2024-04-23 15:35:33 -07:00
collect((new Asset)->getRules())->map(function ($rules) {
return collect($rules)->reject(function ($rule) {
return $rule === 'required';
2024-04-23 15:38:40 -07:00
})->reject(function ($rule) {
return $rule === 'unique_undeleted';
2024-04-23 15:35:33 -07:00
})->values()->all();
})->all(),
);
return $rules2;
}
}