required_with Validation in Laravel 8 Example

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
required_with Validation in Laravel

Hello Dev’s
Now in this tutorial I will show you how to laravel 8 required_with Validation work. So we can easily validate with by using required_with validation rules. The using field under the validation must be present and also it cannot be empty only if any of the other specified fields are present. Here you know required_with Validation in Laravel.

So If there is a dependent item (multiple values??Separated by commas: as like as  foo, bar, …) and also all the item has a value, and also some have specified item has a value (empty or not null).

Controller Code

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Location;

class MainController extends Controller
{
   	public function index()
    {
        return view('form.index');
    }

    public function store(Request $request)
    {
        $request->validate([
            'name' => 'required',
            'surname' => 'required_with:name'
        ]);

        return back();
    }
 }

Create Form

Following the path create in blade file: path=resources/views/from/index.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 6 required_with Validation Example</title>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.css">
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-6 offset-md-3 mt-5">
            <div class="card">
                <div class="card-header bg-light">
                    Laravel 6 required_with Validation Example
                </div>
                <div class="card-body">
                    @if (count($errors) > 0)
                    <div class="row">
                       <div class="col-md-12">
                           <div class="alert alert-danger">
                               @foreach($errors->all() as $error)
                               {{ $error }} <br>
                               @endforeach      
                           </div>
                       </div>
                   </div>
                   @endif
               <form action="{{route ('form.store')}}" method="post">
               <input type="hidden" name="_token" value="{{ csrf_token() }} ">
                  <div class="row">
                     <div class="col-md-12">
                        <div class="form-group">
                           <label>Name:</label>
                           <input type="text"  name="name" value="{{old('name')}}"  class="form-control">
                        </div>
                     </div>
                  </div>
                  <div class="row">
                     <div class="col-md-12">
                        <div class="form-group">
                           <label>Surname:</label>
                           <input type="text" name="surname" value="{{old('name')}}" class="form-control">
                        </div>
                     </div>
                  </div>
                  <div class="row">
                     <div class="col-md-12">
                        <input type="submit" class="btn btn-success btn-block">
                     </div>
                  </div>
               </form>
            </div>
         </div>
      </div>
   </div>
</div>
</body>
</html>

So If name is empty the required check will be skipped,we need to use the surname to conditionally add rules based on the conditions if we want.

Read Also : Laravel 8 date_format Validation Example

Thanks for read. I hope it help you. For more you can follow us on facebook

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →