Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency injection in a custom Laravel Validator class which extends LaravelValidator

I need to gain access to Db repositories inside some validation methods in a custom Laravel Validator class which extends LaravelValidator. How to get it?

My class constructor:

<?php namespace Pongo\Cms\Support\Validators;

use Illuminate\Validation\Validator as LaravelValidator;
use Pongo\Cms\Support\Repositories\PageRepositoryInterface as Page;
use Pongo\Cms\Support\Repositories\ElementRepositoryInterface as Element;

class PongoValidator extends LaravelValidator {

    /**
     * Class constructor
     * @param Page    $page 
     * @param Element $element
     */
     public function __construct($translator, $data, $rules, $messages, Page $page, Element $element)
     {
         $this->page = $page;
         $this->element = $element;
     }

     [...]

and my Validator resolver (as on Laravel docs):

<?php

/**
 * Instantiate CustomValidator class
 */
Validator::resolver(function($translator, $data, $rules, $messages)
{
    return new Pongo\Cms\Support\Validators\PongoValidator($translator, $data, $rules, $messages);
});

How to make it work? Thanks

like image 828
redbaron76 Avatar asked Oct 08 '13 13:10

redbaron76


People also ask

What is the method used for specifying custom messages for validator errors in form request?

After checking if the request failed to pass validation, you may use the withErrors method to flash the error messages to the session. When using this method, the $errors variable will automatically be shared with your views after redirection, allowing you to easily display them back to the user.

What is the method used to configure validation rules in form request laravel?

Laravel Form Request class comes with two default methods auth() and rules() . You can perform any authorization logic in auth() method whether the current user is allowed to request or not. And in rules() method you can write all your validation rule.

What is custom validation in laravel?

Customizing Validation Rules Using Laravel. Share. While you are working with user input and forms, validating the common fields and extracting the exact validation rules makes the code easier to maintain and read. You can make the code powerful by using the Laravel custom validation rule with parameters.


1 Answers

Ok... I found a solution.

My class constructor:

<?php namespace Pongo\Cms\Support\Validators;

use Pongo\Cms\Support\Repositories\PageRepositoryInterface as Page;
use Pongo\Cms\Support\Repositories\ElementRepositoryInterface as Element;

use Illuminate\Validation\Validator as LaravelValidator;

use Config, Media, Str;

class PongoValidator extends LaravelValidator {

    private $page;

    private $element;

/**
 * Class constructor
 * @param Page    $page 
 * @param Element $element
 */
public function __construct($translator, $data, $rules, $messages, Page $page, Element $element)
{
    $this->translator = $translator;
    $this->data = $data;
    $this->rules = $this->explodeRules($rules);
    $this->messages = $messages;

    $this->page = $page;
    $this->element = $element;
}

[...]

and my Validator resolver (with IoC on rescue!!):

<?php

use Pongo\Cms\Support\Validators\PongoValidator;

/**
 * Instantiate CustomValidator class
 */
Validator::resolver(function($translator, $data, $rules, $messages)
{
    $page = App::make('Pongo\Cms\Support\Repositories\PageRepositoryInterface');

    $element = App::make('Pongo\Cms\Support\Repositories\ElementRepositoryInterface'));

    return new PongoValidator($translator, $data, $rules, $messages, $page, $element);
});

Hope it could help anyone else... Bye

like image 176
redbaron76 Avatar answered Nov 15 '22 00:11

redbaron76