50 lines
1.1 KiB
PHP
50 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use Illuminate\Contracts\Validation\ValidationRule;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class StoreProjectRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return $this->user()?->can('create-project') ?? false;
|
|
}
|
|
|
|
protected function prepareForValidation(): void
|
|
{
|
|
$key = Arr::get($this->all(), 'key');
|
|
|
|
if ($key !== null) {
|
|
$this->merge([
|
|
'key' => strtoupper(trim((string) $key)),
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'key' => [
|
|
'required',
|
|
'string',
|
|
'max:5',
|
|
'regex:/^[A-Z0-9]+$/',
|
|
Rule::unique('projects', 'key'),
|
|
],
|
|
'name' => ['required', 'string', 'max:255'],
|
|
];
|
|
}
|
|
}
|