laravel-vue-file-share/app/Http/Requests/StoreFolderRequest.php
2023-10-08 14:20:03 -07:00

47 lines
1.4 KiB
PHP

<?php
namespace App\Http\Requests;
use App\Http\Requests\ParentIDBaseRequest;
use App\Models\File;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
class StoreFolderRequest extends ParentIDBaseRequest
{
// authorization handled by ParentIDBaseRequest
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
// if parent_id was not passed in, validate from root
$parent_id = !$this->parent_id
? File::query()->whereIsRoot()->where('created_by', Auth::id())->first()
: $this->parent_id;
// merge parent ParentIDBaseRequest rules with new rule
return array_merge(parent::rules(),
[
// require folder name, must be unique in the DB, made by the current user, with parent_id, and not marked as deleted
'name' => [
'required',
Rule::unique(File::class, 'name')
->where('created_by', Auth::id())
->where('parent_id', $parent_id)
->whereNull('deleted_at')
]
]
);
}
public function messages()
{
return [
'name.unique' => 'Folder ":input" already exists'
];
}
}