To create and use custom validation rules in Laravel, you can follow these steps:
Define the Custom Rule:
You can define a custom validation rule using a rule object. Create a new class that extends Illuminate\Validation\Rules\Rule
. For example, if you want to validate that a field contains a specific word, you might create a class named ContainsWord
.
use Illuminate\Contracts\Validation\Rule; class ContainsWord implements Rule { private $word; public function __construct($word) { $this->word = $word; } public function passes($attribute, $value) { return stripos($value, $this->word) !== false; } public function message() { return "The :attribute must contain the word :word."; } }
Use the Custom Rule:
To use the custom rule in your controller or form request, you can instantiate the rule and pass it to the validate
method.
use App\Rules\ContainsWord; $request->validate([ 'title' => ['required', new ContainsWord('Laravel')], ]);
Using Custom Rule in Form Request:
If you're using Form Requests, you can include the rule in the rules
method.
use App\Rules\ContainsWord; use Illuminate\Foundation\Http\FormRequest; class StorePostRequest extends FormRequest { public function rules() { return [ 'title' => ['required', new ContainsWord('Laravel')], ]; } }
Using custom validation rules in Laravel offers several benefits:
Reusability:
Expressiveness:
Specificity:
Consistency:
Testability:
To extend Laravel's validation system with your own custom rules, you can follow these approaches:
Using Rule Objects:
Illuminate\Validation\Rules\Rule
. This is the preferred method for complex rules.Using Closures:
For simple rules, you can define a closure within your validation rules.
$request->validate([ 'title' => [ 'required', function ($attribute, $value, $fail) { if (stripos($value, 'Laravel') === false) { $fail('The '.$attribute.' must contain the word "Laravel".'); } }, ], ]);
Extending Validator:
You can extend the Validator
facade to add new rules that can be used throughout your application.
use Illuminate\Support\Facades\Validator; Validator::extend('contains_word', function ($attribute, $value, $parameters, $validator) { $word = $parameters[0]; return stripos($value, $word) !== false; }); Validator::replacer('contains_word', function ($message, $attribute, $rule, $parameters) { return str_replace(':word', $parameters[0], $message); });
You can then use the custom rule in your validation.
$request->validate([ 'title' => 'required|contains_word:Laravel', ]);
There are several resources where you can find examples and tutorials on implementing custom validation rules in Laravel:
Laravel Official Documentation:
Laracasts:
Laravel News:
GitHub:
Stack Overflow:
Laravel Community Blogs:
By leveraging these resources, you can gain a deeper understanding of how to implement custom validation rules in Laravel effectively.
The above is the detailed content of How can I create and use custom validation rules in Laravel?. For more information, please follow other related articles on the PHP Chinese website!