Skip to content

Commit 562f08a

Browse files
committed
refactor(subject): move store & update validation to Form Request classes
1 parent 6a80f6e commit 562f08a

4 files changed

Lines changed: 92 additions & 42 deletions

File tree

‎app/Http/Controllers/Admin/SubjectController.php‎

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace App\Http\Controllers\Admin;
44

55
use App\Http\Controllers\Controller;
6+
use App\Http\Requests\Subject\StoreSubjectRequest;
7+
use App\Http\Requests\Subject\UpdateSubjectRequest;
68
use App\Models\Node;
79
use App\Models\Subject;
810
use Illuminate\Http\Request;
@@ -34,55 +36,17 @@ public function edit(Subject $subject)
3436
}
3537

3638

37-
public function store(Request $request)
39+
public function store(StoreSubjectRequest $request)
3840
{
39-
$validated = $request->validate([
40-
'name' => ['required', 'string', 'max:100', 'min:3', 'unique:subjects,name'],
41-
'tailwind_format' => ['required', 'string', 'max:100'],
42-
'icon' => ['required', 'string', 'max:50'],
43-
'sort_order' => ['nullable', 'integer'],
44-
]);
45-
46-
$subject = new Subject();
47-
48-
$subject->name = $validated['name'];
49-
$subject->tailwind_format = $validated['tailwind_format'];
50-
$subject->slug = Str::slug($validated['name']);
51-
$subject->icon = $validated['icon'];
52-
$subject->sort_order = $validated['sort_order'] ?? 0;
53-
54-
$subject->save();
41+
Subject::create($request->validated());
5542

5643
return redirect()->route('admin.subjects.index');
5744
}
5845

59-
public function update(Request $request, Subject $subject)
46+
public function update(UpdateSubjectRequest $request, Subject $subject)
6047
{
61-
$validated = $request->validate([
62-
'name' => ['sometimes', 'string', 'max:100', 'min:3', Rule::unique('subjects', 'name')->ignore($subject->id),],
63-
'tailwind_format' => ['sometimes', 'string', 'max:100'],
64-
'icon' => ['sometimes', 'string', 'max:50'],
65-
'sort_order' => ['sometimes', 'nullable', 'integer'],
66-
]);
67-
68-
if (isset($validated['name'])) {
69-
$subject->name = $validated['name'];
70-
$subject->slug = Str::slug($validated['name']);
71-
}
72-
73-
if (isset($validated['tailwind_format'])) {
74-
$subject->tailwind_format = $validated['tailwind_format'];
75-
}
76-
77-
if (isset($validated['icon'])) {
78-
$subject->icon = $validated['icon'];
79-
}
80-
81-
if (array_key_exists('sort_order', $validated)) {
82-
$subject->sort_order = $validated['sort_order'] ?? 0;
83-
}
48+
$subject->update($request->validated());
8449

85-
$subject->save();
8650

8751
return redirect()->route('admin.subjects.index');
8852
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Subject;
4+
5+
use Illuminate\Contracts\Validation\ValidationRule;
6+
use Illuminate\Foundation\Http\FormRequest;
7+
use Illuminate\Support\Str;
8+
9+
class StoreSubjectRequest extends FormRequest
10+
{
11+
/**
12+
* Determine if the user is authorized to make this request.
13+
*/
14+
public function authorize(): bool
15+
{
16+
return true;
17+
}
18+
19+
protected function prepareForValidation(): void
20+
{
21+
$this->merge([
22+
'slug' => Str::slug($this->name),
23+
]);
24+
}
25+
26+
/**
27+
* Get the validation rules that apply to the request.
28+
*
29+
* @return array<string, ValidationRule|array<mixed>|string>
30+
*/
31+
public function rules(): array
32+
{
33+
return [
34+
'name' => ['required', 'string', 'max:100', 'min:3', 'unique:subjects,name'],
35+
'slug' => ['required', 'string', 'unique:subjects,slug'],
36+
'tailwind_format' => ['required', 'string', 'max:100'],
37+
'icon' => ['required', 'string', 'max:50'],
38+
'sort_order' => ['required', 'integer'],
39+
];
40+
}
41+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Subject;
4+
5+
use Illuminate\Contracts\Validation\ValidationRule;
6+
use Illuminate\Foundation\Http\FormRequest;
7+
use Illuminate\Support\Str;
8+
use Illuminate\Validation\Rule;
9+
10+
class UpdateSubjectRequest extends FormRequest
11+
{
12+
/**
13+
* Determine if the user is authorized to make this request.
14+
*/
15+
public function authorize(): bool
16+
{
17+
return true;
18+
}
19+
protected function prepareForValidation(): void
20+
{
21+
if ($this->exists('name')) {
22+
$this->merge([
23+
'slug' => Str::slug($this->input('name')),
24+
]);
25+
}
26+
}
27+
/**
28+
* Get the validation rules that apply to the request.
29+
*
30+
* @return array<string, ValidationRule|array<mixed>|string>
31+
*/
32+
public function rules(): array
33+
{
34+
$subject = $this->route('subject');
35+
36+
return [
37+
'name' => ['sometimes', 'string', 'max:100', 'min:3', Rule::unique('subjects', 'name')->ignore($subject->id),],
38+
'tailwind_format' => ['sometimes', 'string', 'max:100'],
39+
'icon' => ['sometimes', 'string', 'max:50'],
40+
'sort_order' => ['sometimes', 'integer'],
41+
'slug' => ['sometimes', 'string'],
42+
];
43+
}
44+
}

‎app/Models/Subject.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
class Subject extends Model
88
{
9+
protected $guarded = [];
910
public function nodes()
1011
{
1112
return $this->hasMany(Node::class);

0 commit comments

Comments
 (0)