Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 33 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| TemplateFormRequest | |
0.00% |
0 / 33 |
|
0.00% |
0 / 3 |
30 | |
0.00% |
0 / 1 |
| authorize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| rules | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
12 | |||
| messages | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Requests; |
| 4 | |
| 5 | use Illuminate\Foundation\Http\FormRequest; |
| 6 | use Illuminate\Validation\Rule; |
| 7 | |
| 8 | class TemplateFormRequest extends FormRequest |
| 9 | { |
| 10 | public function authorize(): bool |
| 11 | { |
| 12 | return true; |
| 13 | } |
| 14 | |
| 15 | public function rules(): array |
| 16 | { |
| 17 | $userId = request()->user()->getKey(); |
| 18 | $rules = []; |
| 19 | switch ($this->method()) { |
| 20 | case 'POST': |
| 21 | $rules = [ |
| 22 | 'title' => 'string', |
| 23 | 'shortcut' => Rule::unique('templates', 'shortcut')->where('user_id', $userId), |
| 24 | 'first_line' => 'sometimes|string|nullable', |
| 25 | 'text' => 'sometimes|string|nullable', |
| 26 | 'html' => 'required|string', |
| 27 | 'category_id' => 'required|string|exists:template_categories,_id', |
| 28 | 'uploads' => 'sometimes|array', |
| 29 | 'type' => 'required|string|in:free,premium', |
| 30 | ]; |
| 31 | break; |
| 32 | |
| 33 | case 'PUT': |
| 34 | $id = request()->route('id'); |
| 35 | $rules = [ |
| 36 | 'title' => 'sometimes|string', |
| 37 | 'shortcut' => Rule::unique('templates', 'shortcut')->where('user_id', $userId)->ignore($id, '_id'), |
| 38 | 'first_line' => 'sometimes|string|nullable', |
| 39 | 'text' => 'sometimes|string|nullable', |
| 40 | 'html' => 'sometimes|required|string', |
| 41 | 'category_id' => 'sometimes|required|string|exists:template_categories,_id', |
| 42 | 'uploads' => 'sometimes|array', |
| 43 | 'type' => 'sometimes|required|string|in:free,premium', |
| 44 | ]; |
| 45 | break; |
| 46 | } |
| 47 | |
| 48 | return $rules; |
| 49 | } |
| 50 | |
| 51 | public function messages(): array |
| 52 | { |
| 53 | return [ |
| 54 | 'shortcut.unique' => 'The FlyPlate has already been taken.', |
| 55 | ]; |
| 56 | } |
| 57 | } |