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
(Co-Founder)We Are Also Recommending You :
- Pay10 Payment Gateway Integration In Laravel 10
- Laravel 12 React Pagination & Search Tutorial (Full Stack Guide 2026)
- Know All About Laravel Mixin Use In Vue Js With Example
- Laravel 6 - QR Code Generator Example
- Laravel 9 CRUD Operation From Scratch Step By Step Guideline
- Laravel 9 File manager integration tutorial
- How to use Date Format Validation in Laravel?
- All Guides About Soft Delete Laravel 8 And 9 With Examples
- Cashfree Payment Gateway Integration In Laravel
- How to Generate PDF File Using DomPDF In Laravel 9 Example