This tutorial is for those who already have basic skills for Laravel. If you are not familiar with Laravel please contact us.

Assuming you have your fields ready.

in your view

{{ Form::text(‘field_a’) }}
{{ Form::text(‘field_b’) }}

in App/ProvidersAppServiceProvider.php add

Validator::extend(’empty_with’, function ($attribute, $value, $parameters, $validator) {
$data = $validator->getData();
return ($value != ” && $data[$parameters[0]] != ”) ? false : true;
});

in your controller insert function

$rules = [
‘field_a’ => ’empty_with:field_b|required_without:field_b’,
‘field_b’ => ’empty_with:field_a|required_without:field_a’
];

$message = [
‘field_a.empty_with’ => “Either field a or field b can be allowed.”,
‘field_b.empty_with’ => “Either field a or field b can be allowed.”,
];

$this->validate($request, $rules, $message);

*required_without is to make sure that both field a and b is not empty.

That’s it!