Everything you need to know about laravel required validations

One of the strong part of the laravel core is the set of validations which validate your application's incoming data. One of the most common validation rule which is used in applications is required. We will see how can we make use of different types of required rules as well as build custom required rules conditionally.

  • Required rule :

As per the laravel's documentation. when we use required rule, the field under validation must be present and also it should be non-empty. Empty includes null value, empty string, empty array and uploaded file with no path.

In below request both first_name and last_name should be present and non-empty.

$request->validate([
    'first_name' => 'required|unique:posts|max:255',
    'last_name' => 'required',
]);
  • Required rule based on other/supporting field(s) :

Many times when designing complex and nested forms, we need to have required validation only if an other incoming data field is present. Now there are multiple cases in this :

  1. When you want just single supporting field to be present, then you can use required_with like below :

In below request the city, state, zipcode and country will be required if address_line is present in the incoming data.

$request->validate([
    'address_line' => 'sometimes|boolean',
    'city' => 'required_with:address_line',
    'state' => 'required_with:address_line',
    'zipcode' => 'required_with:address_line',
    'country' => 'required_with:address_line'
]);
  1. When you want any one of the supporting set of fields just to be present, then you can use required_with and pass multiple fields like below :

In below request the we want to set validation rule for the notify_new_updates field. We can not notify if we do not have either of user phone number, email or fax. So the validation required_with:phone,email,fax signifies that notify_new_updates will be required if one of the field phone, email or fax is present.

$request->validate([
    'name' => 'required|string',
    'phone' => 'sometimes|string',
    'email' => 'sometimes|email',
    'fax' => 'sometimes|string',
    'notify_new_updates' => 'boolean|required_with:phone,email,fax'
]);
  1. When you want all of the supporting set of fields to be present, then you can use required_with_all and pass multiple fields like below :

In below request the validation required_with_all:phone,email,fax signifies that notify_new_updates will be required if all the field phone, email and fax are present.

$request->validate([
    'name' => 'required|string',
    'phone' => 'sometimes|string',
    'email' => 'sometimes|email',
    'fax' => 'sometimes|string',
    'notify_new_updates' => 'boolean|required_with_all:phone,email,fax'
]);
  1. When you want any one of the supporting set of fields NOT to be present, then you can use required_without and pass one or more fields like below :

In below request the use_billing_as_shipping will be required when shipping_address is NOT present.

$request->validate([
    'billing_address' => 'required|string',
    'shipping_address' => 'sometimes|string',
    'use_billing_as_shipping' => 'boolean|required_without:shipping_address'
]);

Similarly you can use required_without_all where it will need sll of the supporting set of fields NOT to be present.

  1. When you want the other field not only to be present but also it should have a very specific value, then you can use required_if like below

In below request the admin_notification_email will be required if and only if the is_admin field is present having value of 1.

$request->validate([
    'is_admin' => 'required|boolean',
    'admin_notification_email' => 'required_if:is_admin,1|email',
]);
  1. When you want the other field not only to be present but it should have a very specific value, then you can use required_if like below

In below request the displayname will be required unless the nickname field is empty.

$request->validate([
    'nickname' => 'sometimes|string',
    'displayname' => 'required_unless:nickname,',
]);
  • Custom and complex required if validation rules :

We sometimes need to have complex set of conditional rule for required if. With laravel it's much easier than you could have imagined.

Laravel's Rule facade has Rule::requiredIf() method which we can use for this purpose. The beautify of this method is, it takes a boolean value or a closure which return a boolean value. This gives us lot of flexibility to have complex logic for required if rule.

  1. requiredIf() with simple boolean :

Below example will make employee_id field required only if current user is an employee and the employee's company is still active.

$request->validate([
    'employee_id' => Rule::requiredIf($request->user()->is_employee && $request->user()->employee->company->is_active)
]);
  1. requiredIf() with closure :

Below example will make golden_discount_voucher field required only if current user is a customer and has orders to fillfull criteria that the customer belongs to golden account category.

$request->validate([
    'golden_discount_voucher' => Rule::requiredIf(function() use($request){

        return  $request->user()->is_customer &&
                $request->user()->customer->orders->where('orders.grand_total', '>', '1000')->count() > 10;
    })
]);