54 lines
1.4 KiB
PHP
54 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Validator;
|
|
|
|
class EditSplitScoreDefinitionRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return auth()->user()->is_admin;
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*
|
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
|
|
return [
|
|
'new_split' => 'sometimes|string|max:255|min:1',
|
|
'split' => 'sometimes|array',
|
|
];
|
|
}
|
|
|
|
public function after(): array
|
|
{
|
|
$splitScoreDefinition = $this->route('splitScoreDefinition');
|
|
|
|
return [
|
|
function (Validator $validator) use ($splitScoreDefinition) {
|
|
if ($this->has('new_split')) {
|
|
$existingSplitNames = collect($splitScoreDefinition->splits)
|
|
->pluck('name')
|
|
->toArray();
|
|
|
|
if (in_array($this->new_split, $existingSplitNames)) {
|
|
$validator->errors()->add(
|
|
'new_split',
|
|
'This split name already exists in this definition.'
|
|
);
|
|
}
|
|
}
|
|
},
|
|
];
|
|
}
|
|
}
|