Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

laravel
communitycreator

What is the "required_without" validation rule in Laravel?

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The required_without rule is used for form validation in Laravel. This rule is similar to frontend checks that mark form input fields as required.

Usage of the required_without validation rule

The required_without validation rule can be seen as an or in a query language statement, in which execution continues if any condition is met.

The code below shows how to use the required_without validation rule:

public function store(Request $request)
{
 	$request->validate([
	  	"primary_contact" =>"required",
	  	"secondary_contact" =>"required_without:primary_contact"
 	]);

  	dd("Done!");
}

Explanation

In the code above, we set primary_contact as required and secondary_contact as required_without. secondary_contact also references the primary_contact field.

If the primary_contact field is filled and the secondary_contact field is empty, then execution continues.

Similarly, if the primary_contact field is empty and the secondary_contact field is filled, the execution again continues.

In the case of the code above, there is only an error when both the primary_contact and secondary_contact fields are empty.

RELATED TAGS

laravel
communitycreator

CONTRIBUTOR

Chinweuba Elijah Azubuike

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring