File name required when file upload laravel validation rules

  • 22-12-2022
  • 480
  • Laravel 9
  • Haresh Chauhan

File name field required when the file uploads laravel validation rule,using required_with laravel validation rule make file name field required when the user upload file else the file and file name both field will be nullable, in this post you will validate how to make the file name field required when the file field is filled using laravel validation rules, This is a very simple quick validation rule method required_with

The below laravel example will help you to make the required file name filed when the user enters a file or document, generally document field is optional when a user enters their data, but when a user likes to upload the document that time they need to specify which document they entered in the form so the verification team understands that document type very well.

So when the user does not like to upload a document not mandatory document name field, but when a user likes to upload a document file that time is necessary to take its file type name that time it will require a field using laravel validation rules.

Here is a simple example given how to make the required file name when a file or document is uploaded by the user using laravel validation rules.

public function store(Request $request)
    $validator = $request->validate([
        'doc_1' => 'nullable|mimes:doc,txt,git,png,jpg,jpeg,pdf|max:2048',
        'doc_1_text' => 'nullable|required_with:doc_1,empty|max:100',
        'doc_1_text.required' => 'The doc 1 name field is required.',
        'doc_1_text.required_with' => 'The doc 1 name field is required when doc 1 available.',

We always thanks to you for reading our blogs.


Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us


Haresh Chauhan


We Are Also Recommending You :