Skip to content

Commit

Permalink
add validation rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiritin committed Jul 21, 2024
1 parent 88a7941 commit 26ad18f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
5 changes: 3 additions & 2 deletions app/Http/Requests/BadgeCreateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace App\Http\Requests;

use App\Rules\AlphaNumSpaceRule;
use Illuminate\Foundation\Http\FormRequest;

class BadgeCreateRequest extends FormRequest
{
public function rules(): array
{
return [
'species' => ['required','string'],
'name' => ['required','string','max:32'],
'species' => ['required','string','max:32', new AlphaNumSpaceRule()],
'name' => ['required','string','max:32', new AlphaNumSpaceRule()],
'image' => [
'required',
'image',
Expand Down
5 changes: 3 additions & 2 deletions app/Http/Requests/BadgeUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace App\Http\Requests;

use App\Rules\AlphaNumSpaceRule;
use Illuminate\Foundation\Http\FormRequest;

class BadgeUpdateRequest extends FormRequest
{
public function rules(): array
{
return [
'species' => ['required','string'],
'name' => ['required','string','max:32'],
'species' => ['required','string','max:32', new AlphaNumSpaceRule()],
'name' => ['required','string','max:32', new AlphaNumSpaceRule()],
'image' => [
'nullable',
'image',
Expand Down
17 changes: 17 additions & 0 deletions app/Rules/AlphaNumSpaceRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Str;

class AlphaNumSpaceRule implements ValidationRule
{
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (! preg_match('/^[a-zA-Z0-9\s\-\_\.]+$/u', $value)) {
$fail(Str::ucfirst($attribute) . ' can only contain letters, numbers, spaces and the following symbols: - . _');
}
}
}

0 comments on commit 26ad18f

Please sign in to comment.