Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

theUnforgiven's avatar

Using Validation within a Repository

Thinking of doing my validation within one of my repositories so Is it......

1 a good idea to use validation with a repository 2. How would one achieve this? 3. Keeps controller skinny, right?

Possibly a good question for @JeffreyWay

0 likes
5 replies
thepsion5's avatar

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:

  1. The request comes in and calls the store() method on UserController
  2. The controller calls the createUserFromArray() method on UserService
  3. The service calls the fails() method on UserValidator, which contains the user's validation rules
  4. The fails() method returns false
  5. The service creates a new User instance
  6. The service calls save() on UserRepository and passes it the new instance
  7. The service returns the result back to the controller
  8. The controller redirects to another page where the user is displayed

If the user data is invalid:

  1. The request comes in and calls the store() method on UserController
  2. The controller calls the createUserFromArray() method on UserService
  3. The service calls the fails() method on UserValidator, which contains the user's validation rules
  4. The fails() method returns true
  5. The service throws a new ValidationException instance, with the validation messages from UserValidator
  6. The controller catches the exception and retrieves the error messages
  7. 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.

4 likes
theUnforgiven's avatar

@thepsion5 It's quite a big project so a 'service class' would be ideal, question is how do I go about creating this class, in terms of where should I put then how do I call it to check for validation then if validation passes carry on down the script?

theUnforgiven's avatar

Where are you getting $this->validator->validate from would I use Validator at the top or this class?

thepsion5's avatar

I've updated the example and added some code for the validator class.

Please or to participate in this conversation.