I'm having a curious problem that probably stems from my lack of knowledge of the IOC! I'm injecting a custom validator into my base model and then using model events to validate on save/update ( see below ).
The code works and I can access an array of validation errors in my controllers except where there is a model model I instantiate in App::before.
If I remove it from App::before , all is fine. Otherwise the validation fails but the error messages get set on the validator class but disappear upon exiting the model event?
Can anyone shed any light on this?
Thanks
Leo
// filters.php
App::before(function ($request) {
$sale_next = App::make('Denhams\Repositories\Sale\SaleRepositoryInterface');
// get forthcoming sale and load into session
}
// SaleController.php
public function update($id)
{
$model = $this->sale->find($id);
$input = array_except(Input::all(), '_method');
$model->fill($input);
if ($this->sale->save($model)) {
return Redirect::route('admin.sales.index', $id);
}
// NO ERRORS!!!
var_dump($this->sale->validator->errors());
}
class EloquentDbSaleRepository extends EloquentDbRepository implements SaleRepositoryInterface {
//public $model;
//public $validator;
public function __construct(Sale $model, SaleValidator $validator) {
parent::__construct($model,$validator);
}
abstract class EloquentDbRepository
{
protected $model;
protected $query;
protected $validator;
public function __construct($model,$validator)
{
$this->model = $model;
$this->validator = $validator;
$class_name = get_class($this->model);
$class_name::creating(function($model){
return $this->validator->validateCreate($model);
});
$class_name::updating(function($model){
//POPULATED WITH ERRORS
var_dump($this->validator->errors());
return $this->validator->validateUpdate($model);
});
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community