It depends on what you're thinking of when you're using a Repository.
The most correct use of the repository pattern means that it only cares about storing data. By that definition, anything you give to it should already be valid, so your validation should go into a service class or command handler. The controller would call the service/handler, and the service/handler would validate the data and call the repository.
Here's an example:
//UserController.php
public function store($id)
{
try {
$user = $this->userService->createUserFromArray( Input::all() );
} catch(ValidationException $e) {
return Redirect::back()->withErrors( $e->getErrors() )->withInput();
}
return Redirect::route('users.show', $user->id);
}
//UserService.php
public function createUserFromArray(array $userData)
{
if( $this->validator->validate($userData) ) {
throw new ValidationException('Could not create User: Please check your data and try again.', 0, null, $validator->messages() );
}
$user = new User($userData);
$this->repository->save($user);
return $user;
}
//UserValidator.php
public function fails(array $input)
{
$validator = $this->factory->make($input, $this->rules, $this->messages);
$result = $validator->fails($input);
$this->errors = $validator->messages();
return $result;
}
public function messages()
{
return $this->errors;
}
So, this is what happens if the user data is valid:
- The request comes in and calls the
store()method onUserController - The controller calls the
createUserFromArray()method onUserService - The service calls the
fails()method onUserValidator, which contains the user's validation rules - The
fails()method returns false - The service creates a new User instance
- The service calls
save()onUserRepositoryand passes it the new instance - The service returns the result back to the controller
- The controller redirects to another page where the user is displayed
If the user data is invalid:
- The request comes in and calls the
store()method onUserController - The controller calls the
createUserFromArray()method onUserService - The service calls the
fails()method onUserValidator, which contains the user's validation rules - The
fails()method returns true - The service throws a new
ValidationExceptioninstance, with the validation messages fromUserValidator - The controller catches the exception and retrieves the error messages
- The controller redirects back, with input and the error messages
If you're working on a smaller project that may be overkill, in which case I would be fine with putting it in the repository.