I needed to create a validation rule in Laravel that would accept either of the two inputs, but not both and at least one had to be supplied. The closer I could get while keeping complexity at minimum was to use the required_without Laravel validation rule like this: public function rules(): array { return [ input1 => [ 'required_without:input2', ], input2 => [ 'required_without:input1', ], ]; } However this was still not sufficient as this is not a XOR operation, this is a plain OR operation: 000 011 101 111

The only drawback was the validation message returned: {"message": "The given data was invalid.", "errors": {"qrcode": ["validation.prohibits"], "code": ["validation.prohibits"]} }

To reduce the code repetition, the result could look like this: public function messages(): array { return [ ...$this->customMessage('input1', 'input2'), ...$this->customMessage('input2', 'input1'), ]; } private function customMessage(string $input, string $otherInput): array { return ["$input.prohibits" => "The $input field is prohibited when $otherInput is present."
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners