laravel-vue-file-share/app/Http/Requests/SharedFiles.php
2023-10-17 22:45:04 -07:00

59 lines
1.7 KiB
PHP

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Database\Query\Builder;
use App\Models\File;
class SharedFiles extends FormRequest
{
public ?File $parent = null;
private ?File $match = null;
public function authorize(): bool
{
// gets first file (main folder) with parent_id as id
$this->parent = File::query()->where('id', $this->input('parent_id'))->first();
// continue
return true;
}
protected function prepareForValidation() {
// cast to boolean
$this->merge([
'all' => filter_var($this->all, FILTER_VALIDATE_BOOL),
]);
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
if ($this->parent) {
// validate
return [
'parent_id' => [
Rule::exists(File::class, 'id')
->where(function (Builder $query) {
return $query
->where('is_folder', '1'); // must be a folder
}
)
],
'all' => 'bool', // all or not
'Ids.*' => Rule::exists('files', 'id'), // check that files exist in database
];
}
else {
return [
'all' => 'bool', // all or not
'Ids.*' => Rule::exists('files', 'id'), // check that file exists in database
];
}
}
}