setnemo / autogenerated-messages
Trait for customizing default messages by type for Laravel Validation
Installs: 1 777
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 1
Forks: 10
Open Issues: 1
Requires
- php: ^7.4|~8.0.0|~8.1
Requires (Dev)
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: *
- vimeo/psalm: ^4.7
README
This project will help to generate automatically error messages based on your validation rules in the Laravel project.
How it works?
You install the package:
composer require setnemo/autogenerated-messages
Then, in your Request class (which inherits from FormRequest), add the trait:
<?php declare(strict_types=1); namespace Awesome\Requests; use Illuminate\Foundation\Http\FormRequest; class NewRequest extends FormRequest { use DefaultMessages; /** Add use trait */ public function rules(): array { return [ 'name' => 'required|string|max:120', 'start_date' => 'required|integer', 'price' => 'nullable|numeric|min:0 ', 'pay_link' => 'nullable|string|url|max:256', 'video' => 'nullable|mimes:mp4,mov,avi', 'confidentiality' => 'required|string|in:public,personal', ]; } }
It's all! Error messages will be generated based on your rules.
Example:
<?php var_export($newRequest->message());
Result:
[ 'name.required' => 'name is required', 'name.string' => 'Value for name must be string', 'name.max' => 'Maximal value for name is 120', 'start_date.required' => 'start_date is required', 'start_date.integer' => 'Value for start_date must be integer', 'price.numeric' => 'Key price must be numeric', 'price.min' => 'Minimal value for price is 0', 'pay_link.string' => 'Value for pay_link must be string', 'pay_link.url' => 'Key pay_link must be valid url', 'pay_link.max' => 'Maximal value for pay_link is 256', 'video.mimes' => 'Allowed formats for video: mp4, mov, avi', 'confidentiality.required' => 'confidentiality is required', 'confidentiality.string' => 'Value for confidentiality must be string', 'confidentiality.in' => 'Allowed values for confidentiality: public, personal', ];
Supported types
Right now project supported next validation types:
[ 'required', 'integer', 'numeric', 'string', 'url', 'in', 'not_in', 'min', 'max', 'mimes', 'email', 'unique', 'json', 'image', 'accepted', 'array', 'boolean', 'regex', 'exists', 'uuid', 'after', 'ip', 'ipv4', 'ipv6', 'mac_address', 'starts_with', 'ends_with', 'doesnt_start_with', 'doesnt_end_with', 'multiple_of', 'same' ];
Also, you can add another Laravel Validation types, just create fork this project and create pull request.
Or add in your project:
<?php $this->addRuleNames(['test_rule']); $this->addRulesToMessages(['test_rule' => 'test_message']);
Contributing Guide
Please check Contributing Guide