47 lines
1.3 KiB
PHP
47 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use App\Http\Requests\ParentId;
|
|
use App\Models\File;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class NewFolder extends ParentId
|
|
{
|
|
// authorization handled by ParentId
|
|
|
|
/**
|
|
* 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'
|
|
];
|
|
}
|
|
}
|