diff --git a/app/Http/Controllers/TagsController.php b/app/Http/Controllers/TagsController.php index b9c2c9db..24292f41 100644 --- a/app/Http/Controllers/TagsController.php +++ b/app/Http/Controllers/TagsController.php @@ -73,9 +73,9 @@ public function create(): View|ViewContract /** * Save the tag in the database. */ - public function store(Request $request): RedirectResponse + public function store(TagUpdateRequest $request): RedirectResponse { - Tag::findOrCreate(preg_split('/\r\n|\r|\n/', $request->tag_name ?? ''), $request->tag_type); + Tag::findOrCreate($request->name, $request->type); return redirect()->route('tags.table') ->with('flash_message', 'Added tags'); diff --git a/app/Http/Requests/TagUpdateRequest.php b/app/Http/Requests/TagUpdateRequest.php index d27b73f5..db6d0064 100644 --- a/app/Http/Requests/TagUpdateRequest.php +++ b/app/Http/Requests/TagUpdateRequest.php @@ -2,50 +2,86 @@ namespace App\Http\Requests; +use App\Student; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Rule; class TagUpdateRequest extends FormRequest { + public array $tag_types = []; + /** * Determine if the user is authorized to make this request. - * - * @return bool */ - public function authorize() + public function authorize(): bool { return true; } /** * Get the validation rules that apply to the request. - * - * @return array */ - public function rules() + public function rules(): array { $locale = app()->getLocale(); + $this->tag_types += [ + "App\\Profile", + ...Student::participatingSchools()->keys()->map(fn($shortname) => "App\\Student\\{$shortname}")->all(), + ]; + + $tag_field = $this->hasMultipleTags() ? 'name.*' : 'name'; + return [ - 'type' => 'required', - 'name' => [ - 'required', - 'string', - 'max:100', - Rule::unique('tags', 'name->'.$locale) - ->where('type', $this->input('type')) - ->ignore($this->input('id')) - ], + 'type' => [ + 'required', + 'string', + Rule::in($this->tag_types), + ], + $tag_field => [ + 'required', + 'string', + 'max:100', + Rule::unique('tags', 'name->' . $locale) + ->where('type', $this->input('type')) + ->ignore($this->route()->parameters['tag'] ?? null), + ], ]; } - public function messages() + protected function prepareForValidation(): void + { + // split string of multiple tag names into an array for easier validation + if ($this->hasMultipleTags()) { + $this->merge([ + 'name' => preg_split('/\r\n|\r|\n/', $this->name ?? ''), + ]); + } + } + + protected function hasMultipleTags(): bool + { + return $this->routeIs('tags.store'); + } + + public function messages(): array { + $types_allowed = implode(', ', $this->tag_types); + return [ - 'name.required' => 'The tag name is required.', - 'name.unique' => 'The tag name provided already exists.', - 'name.max' => 'The tag name provided exceeds maximum length of 100 characters.', + 'type.in' => "The tag types allowed are: {$types_allowed}", ]; } + /** + * Get custom attribute names for validator errors. + */ + public function attributes(): array + { + return [ + 'name' => 'tag name', + 'type' => 'tag type', + ]; + } + } diff --git a/resources/views/tags/create.blade.php b/resources/views/tags/create.blade.php index d9b3ba5b..884a8cc6 100644 --- a/resources/views/tags/create.blade.php +++ b/resources/views/tags/create.blade.php @@ -22,17 +22,18 @@

Add Tags

+ @include('errors/list') {!! Form::open(['route' => 'tags.store']) !!}
- {!! Form::label('tag_name', 'Tag name(s)', ['class' => 'form-label']) !!} + {!! Form::label('name', 'Tag name(s)', ['class' => 'form-label']) !!} One tag per line - {!! Form::textarea('tag_name', null, ['class' => 'form-control', 'required']) !!} + {!! Form::textarea('name', null, ['class' => 'form-control', 'required']) !!}
- {!! Form::label('tag_type', 'Tag type', ['class' => 'form-label']) !!} + {!! Form::label('type', 'Tag type', ['class' => 'form-label']) !!} e.g. App\Profile, App\Student, and etc. - {!! Form::text('tag_type', null, ['class' => 'form-control', 'required']) !!} + {!! Form::text('type', null, ['class' => 'form-control', 'required']) !!}
diff --git a/resources/views/tags/edit.blade.php b/resources/views/tags/edit.blade.php index faf96c78..0b0556ce 100644 --- a/resources/views/tags/edit.blade.php +++ b/resources/views/tags/edit.blade.php @@ -22,7 +22,7 @@

Edit Tag {{ $tag->name }}

- @include('errors/has') + @include('errors/list') {!! Form::model($tag, ['method' => 'PATCH','route' => ['tags.updateTag', $tag], 'class' => 'form-horizontal' ]) !!}