There are probably more workarounds but what I'd do in that case is create a custom validator, that hold rules and messages. For instance:
class UserValidator
{
/**
* @param array $data
* @return array of errors
*/
public function validate(array $data)
{
/** @var \Illuminate\Validation\Validator $validator */
$validator = Validator::make($data, $this->rules(), $this->messages());
return $validator->errors()->toArray();
}
/**
* @return array
*/
protected function rules()
{
return [
// rules
];
}
/**
* @return array
*/
protected function messages()
{
return [
// messages
];
}
}
You then can inject this validator to controller and ask it for errors
public function create(UserValidator $validator, Request $request)
{
$errors = $validator->validate($request->all());
if (sizeof($errors)) {
// validation failed, do something
}
// do something else
}
Actually for more reusable code you can extract this part to a superclass so each custom validator only holds data about rules and messages or so.
/**
* @param array $data
* @return array of errors
*/
public function validate(array $data)
{
/** @var \Illuminate\Validation\Validator $validator */
$validator = Validator::make($data, $this->rules(), $this->messages());
return $validator->errors()->toArray();
}