Support the ongoing development of Laravel.io →
IOC Validation Architecture

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);
        });
    }
Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

leoden leoden Joined 13 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.