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

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners