0

Anyone use Ardent in Laravel with the repository pattern and have it "auto-hydrate" relations on save? If so, do the rules need to be in the repository or can they be in a separate Validator service?

user3061986
  • 75
  • 1
  • 9

1 Answers1

1

The basic idea of Ardent is autovalidation done in the model itself. However if you want to make your app as robust as possible it's better to use validation services. In the end you can use the service (or even pass it's internal $rules) wherever you wish so it's totally DRY.

EDIT:

Suppose you have such a validation service

namespace App\Services\Validators;
class UserValidator extends Validator {

  /**
   * Validation rules
   */
  public static $rules = array(
    'username' => array('required'),
    'email' => array('required','email'),
    'password' => array('required','min:12','confirmed'),
    'password_confirmation' => array('required','min:12'),
  );

}

in a repository you can do

public function store()
{
  $v = new App\Services\Validators\UserValidator;

  if($v->passes())
  {
    $this->user->create($input);

    return true
  }

  return Redirect::back()->withInput()
    ->withErrors($v->getErrors());
}

in an Ardent model you can just modify the rules directly

Ardent::$rules = UserValidator::$rules

Check out Ardent docs and you might find this article on validation interesting, the code above is based on that article.

Gadoma
  • 6,475
  • 1
  • 31
  • 34
  • OK, but can is there a way to get Ardent to see rules in a repository or validation service (which I currently have)? – user3061986 Jan 22 '14 at 14:13