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
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With