scybulski / laravel-validator-color
Validate that a certain value is a valid CSS color string in Laravel
4.4.2
2023-12-04 17:23 UTC
Requires
- php: >=7.4 <8.4
- illuminate/support: >=6.0 <11.0
- illuminate/validation: >=6.0 <11.0
Requires (Dev)
- nunomaduro/larastan: ^1.0|^2.5
- orchestra/testbench: ^6.0|^7.0|^8.0
- phpunit/phpunit: ^9.0|^10.0
README
This package will let you validate that a certain value is a valid CSS color string.
Installation
Install via composer - In the terminal:
composer require scybulski/laravel-validator-color
Usage
You can use this package either by passing strings to Validator or using Rule Objects.
Validator strings
You need to register a service provider by adding the following to the providers
array in your config/app.php
scybulski\Validator\Color\ServiceProvider::class
Then you can use the rules in a Controller or Form Request as following.
use Illuminate\Foundation\Http\FormRequest; class MyClass extends FormRequest { public function rules() { return [ 'color' => 'required|other_rule|color', 'color_hex' => 'required|other_rule|color_hex', 'color_keyword' => 'required|other_rule|color_keyword', 'color_long_hex' => 'required|other_rule|color_long_hex', 'color_rgb' => 'required|other_rule|color_rgb', 'color_rgba' => 'required|other_rule|color_rgba', 'color_rgb_any' => 'required|other_rule|color_rgb_any', 'color_short_hex' => 'required|other_rule|color_short_hex', ]; } }
Rule Objects
You can define validation rules in a Controller or Form Request as following.
use Illuminate\Foundation\Http\FormRequest; use scybulski\Validator\Color\Rules\Color; use scybulski\Validator\Color\Rules\ColorHex; use scybulski\Validator\Color\Rules\ColorKeyword; use scybulski\Validator\Color\Rules\ColorLongHex; use scybulski\Validator\Color\Rules\ColorRgb; use scybulski\Validator\Color\Rules\ColorRgba; use scybulski\Validator\Color\Rules\ColorRgbAny; use scybulski\Validator\Color\Rules\ColorShortHex; class MyClass extends FormRequest { public function rules() { return [ 'color' => ['required', 'other_rule', new Color], 'color_hex' => ['required', 'other_rule', new ColorHex], 'color_keyword' => ['required', 'other_rule', new ColorKeyword], 'color_long_hex' => ['required', 'other_rule', new ColorLongHex], 'color_rgb' => ['required', 'other_rule', new ColorRgb], 'color_rgba' => ['required', 'other_rule', new ColorRgba], 'color_rgb_any' => ['required', 'other_rule', new ColorRgbAny], 'color_short_hex' => ['required', 'other_rule', new ColorShortHex], ]; } }