10

How to Validate One of Two Fields is Not Empty in Laravel Validation

 3 years ago
source link: https://www.codexworld.com/how-to/validate-one-of-two-fields-is-not-empty-in-laravel-validation/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Home  »  How To Guides  »  Laravel   »   How to Validate One of Two Fields is Not Empty in Laravel Validation

How to Validate One of Two Fields is Not Empty in Laravel Validation


The Validator class helps to validate input fields in Laravel. There are various validation rules available to validate fields. The required_without validation rule is used to check whether one of the other fields must be present and not empty.

Use the required_without rule in Laravel Validation, to validate one of two fields must be filled.

$validator = Validator::make( 
    $this->request->all(),
    [
        'email' => 'required_without:business_email',
        'business_email' => 'required_without:email'
    ],
    [ 
        'email.required_without' => 'Please provide your personal email or business email.',
        'business_email.required_without' => 'Please provide your business email or personal email.',
    ]
);

If one field is required more than two fields, use required_without_all:foo,bar,…

Leave a reply

Cancel

Comment*

Your Name*

Your Email*

Your Website


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK