Skip to content

Commit

Permalink
close #3121 Fixed:
Browse files Browse the repository at this point in the history
  • Loading branch information
cuneytsenturk committed Feb 9, 2024
1 parent a981c78 commit 226c75c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/Http/Requests/Setting/Category.php
Expand Up @@ -18,7 +18,7 @@ public function rules()
return [
'name' => 'required|string',
'type' => 'required|string|in:' . $types->implode(','),
'color' => 'required|string',
'color' => 'required|string|colour',
];
}
}
4 changes: 4 additions & 0 deletions app/Http/Requests/Setting/Setting.php
Expand Up @@ -49,6 +49,10 @@ public function rules()
$rules['number_next'] = 'required|integer';
}

if ($this->request->has('color')) {
$rules['color'] = 'required|string|colour';
}

return $rules;
}

Expand Down
36 changes: 36 additions & 0 deletions app/Providers/Validation.php
Expand Up @@ -4,6 +4,7 @@

use App\Models\Setting\Currency;
use Illuminate\Support\ServiceProvider as Provider;
use Illuminate\Support\Str;
use Validator;

class Validation extends Provider
Expand Down Expand Up @@ -68,6 +69,41 @@ public function boot()
},
trans('validation.custom.invalid_extension')
);

Validator::extend('colour', function ($attribute, $value, $parameters, $validator) {
$status = false;

$colors = ['gray', 'red', 'yellow', 'green', 'blue', 'indigo', 'purple', 'pink'];
$variants = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900];

foreach ($colors as $color) {
if (! Str::contains($value, $color)) {
continue;
}

foreach ($variants as $variant) {
$name = $color . '-' . $variant;

if (Str::contains($value, $name)) {
$status = true;

break;
}
}

if ($status) {
break;
}
}

if (! $status && Str::contains($value, '#')) {
$status = true;
}

return $status;
},
trans('validation.custom.invalid_colour')
);
}

/**
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en-GB/validation.php
Expand Up @@ -156,6 +156,7 @@
'invalid_amount' => 'The amount :attribute is invalid.',
'invalid_extension' => 'The file extension is invalid.',
'invalid_dimension' => 'The :attribute dimensions must be max :width x :height px.',
'invalid_colour' => 'The :attribute colour is invalid.',
],

/*
Expand Down

0 comments on commit 226c75c

Please sign in to comment.