Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/Http/Controllers/TagsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
74 changes: 55 additions & 19 deletions app/Http/Requests/TagUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
}

}
9 changes: 5 additions & 4 deletions resources/views/tags/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@
<div class="container">
<h1><i class="fas fa-tags" aria-hidden="true"></i> Add Tags</h1>

@include('errors/list')
{!! Form::open(['route' => 'tags.store']) !!}

<div class="mb-3">
{!! Form::label('tag_name', 'Tag name(s)', ['class' => 'form-label']) !!}
{!! Form::label('name', 'Tag name(s)', ['class' => 'form-label']) !!}
<small class="text-muted">One tag per line</small>
{!! Form::textarea('tag_name', null, ['class' => 'form-control', 'required']) !!}
{!! Form::textarea('name', null, ['class' => 'form-control', 'required']) !!}
</div>
<div class="mb-3">
{!! Form::label('tag_type', 'Tag type', ['class' => 'form-label']) !!}
{!! Form::label('type', 'Tag type', ['class' => 'form-label']) !!}
<small class="text-muted">e.g. App\Profile, App\Student, and etc.</small>
{!! Form::text('tag_type', null, ['class' => 'form-control', 'required']) !!}
{!! Form::text('type', null, ['class' => 'form-control', 'required']) !!}
</div>

<button type="submit" class="btn btn-primary edit-button">Submit</button>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/tags/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<div class="container">
<h1><i class="fas fa-tags" aria-hidden="true"></i> Edit Tag {{ $tag->name }}</h1>

@include('errors/has')
@include('errors/list')

{!! Form::model($tag, ['method' => 'PATCH','route' => ['tags.updateTag', $tag], 'class' => 'form-horizontal' ]) !!}

Expand Down
Loading