Validation Rule with Some Conditions

If your validation rules depend on some condition, you can modify the rules by adding withValidator() to your FormRequest class, and specify your custom logic there. Like, if you want to add validation rule only for some user role.

1use Illuminate\Validation\Validator;
2class StoreBlogCategoryRequest extends FormRequest {
3 public function withValidator(Validator $validator) {
4 if (auth()->user()->is_admin) {
5 $validator->addRules(['some_secret_password' => 'required']);
6 }
7 }
8}

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord