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."