Laravel custom serviceprovider for validator - php

I want to override a method (isValidatable) in the Illuminate\Validation\Validator class. I have done this by creating a class (outside Illuminate) that extends the Validator and only overrides the isValidatable method.
I think this will work, except I'm not sure how to create the service provider for the Validator class (or actually CustomLaravelValidator class). I have created service providers before, but there seems to be going on a lot inside the Validator serviceprovider (Illuminate\Validation\ValidationServiceProvider). Therefore I don't have a clue on how my custom service provider for this class should look like.
This is my CustomLaravelValidator class:
<?php namespace API\Extensions\Core;
use Illuminate\Validation\Validator;
class CustomLaravelValidator extends Validator {
/**
* Determine if the attribute is validatable.
*
* #param string $rule
* #param string $attribute
* #param mixed $value
* #return bool
*/
protected function isValidatable($rule, $attribute, $value)
{
// Validate integers on empty strings as well
if($rule == 'IntStrict')
{
return true;
}
return $this->presentOrRuleIsImplicit($rule, $attribute, $value) &&
$this->passesOptionalCheck($attribute);
}
}
This is the default ValidationServiceProvider from Laravel:
<?php namespace Illuminate\Validation;
use Illuminate\Support\ServiceProvider;
class ValidationServiceProvider extends ServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* #var bool
*/
protected $defer = true;
/**
* Register the service provider.
*
* #return void
*/
public function register()
{
$this->registerPresenceVerifier();
$this->app->bindShared('validator', function($app)
{
$validator = new Factory($app['translator'], $app);
// The validation presence verifier is responsible for determining the existence
// of values in a given data collection, typically a relational database or
// other persistent data stores. And it is used to check for uniqueness.
if (isset($app['validation.presence']))
{
$validator->setPresenceVerifier($app['validation.presence']);
}
return $validator;
});
}
/**
* Register the database presence verifier.
*
* #return void
*/
protected function registerPresenceVerifier()
{
$this->app->bindShared('validation.presence', function($app)
{
return new DatabasePresenceVerifier($app['db']);
});
}
/**
* Get the services provided by the provider.
*
* #return array
*/
public function provides()
{
return array('validator', 'validation.presence');
}
}
Can anyone tell me how my custom serviceprovider have to look like?

Your service provider doesn't need to mimic the native Validator service provider. You just need to register your custom validator using the resolver method on the validator factory.
use API\Extensions\Core\CustomLaravelValidator;
class CustomValidationServiceProvider extends ServiceProvider {
public function boot()
{
$this->app['validator']
->resolver(function($translator, $data, $rules, $messages)
{
return new CustomLaravelValidator(
$translator,
$data,
$rules,
$messages
);
});
}
}
That's it...

Related

Binding the dependency of a Laravel Service Provider inside the provider itself?

I am just starting to get the hang of Service Providers and the IoC container, however one thing is confusing me. I have a SpamServiceProvider that requires two other classes to function. However one of those classes, InvalidKeywords, has a array $blacklist parameter which needs to be passed to its constructor.
If I register that class in the AppServiceProvider and pass in the $blacklist array, everything works fine. However, if I try to bind the class in the SpamServiceProvider instead it will not inject the $blacklist into InvalidKeywords constructor.
So I guess my question is why is this? And is there a way to keep bindings like this together in a single container or do I simply have to bind InvalidKeywords inside the AppServiceProvider?
This works
class SpamServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* #var bool
*/
protected $defer = true;
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register services.
*
* #return void
*/
public function register()
{
$this->app->bind(SpamManager::class, function ($app) {
return new SpamManager(new InvalidKeywords, new RepeatedCharacters);
});
}
}
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app->bind(InvalidKeywords::class, function ($app) {
return new InvalidKeywords(config('spam.blacklist'));
});
}
}
This does not work
class SpamServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* #var bool
*/
protected $defer = true;
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register services.
*
* #return void
*/
public function register()
{
$this->app->bind(InvalidKeywords::class, function ($app) {
return new InvalidKeywords(config('spam.blacklist'));
});
$this->app->bind(SpamManager::class, function ($app) {
return new SpamManager(new InvalidKeywords, new RepeatedCharacters);
});
}
}
In the second case you're not resolving the InvalidKeywords class from the container, simply creating a new instance. Instead, try using app or resolve helpers when creating the SpamManager:
$this->app->bind(SpamManager::class, function ($app) {
return new SpamManager(resolve(InvalidKeywords::class), resolve(RepeatedCharacters::class));
});
// or
$this->app->bind(SpamManager::class, function ($app) {
return new SpamManager(app(InvalidKeywords::class), app(RepeatedCharacters::class));
});
I would create a singleton with InvalidKeywords as well:
$this->app->singleton(InvalidKeywords::class, function ($app) {
return new InvalidKeywords(config('spam.blacklist'));
});

Laravel Validation Extend Not Firing

I don't think that my validator extentions are working, but I can't fathom why.
Service Provider.
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
Validator::extend('fail', function ($attribute, $value, $parameters, $validator) {
return false;
});
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
}
}
I know the Service Provider's boot is firing because if I dd(); inside the boot method I get output. If I add a dd(); to the extend closure function, I do not get any output.
Request
class SaveOrder extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
$rules = [
'customer_id' => 'in:' . Customer::getImplodedCurrentTeamKeys(),
'pin' => 'fail'
];
return $rules;
}
}
I know the request is validating correctly because if I change the rule to 'pin' => 'required' and don't give in put I get a fail.
Why is my custom validation rule not working?
I found my solution at the very bottom of the Laravel Validation docs page: (https://laravel.com/docs/5.4/validation)
For a rule to run even when an attribute is empty, the rule must imply that the attribute is required. To create such an "implicit" extension, use the Validator::extendImplicit() method:
By changing the method from extend to extendImplicit , my problem was solved.
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
Validator::extendImplicit('fail', function ($attribute, $value, $parameters, $validator) {
return false;
});
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
}
}

How to use method injection with App::call in Laravel

I am using Laravel 5.2 and wrote my own Service Provider. I want to inject a Request object into the register method.
The base problem is that I want to call different service container depending on a special request param - all the service container implementing the same interface/contract of course.
The error message I am getting is:
ReflectionException in Container.php line 559:
Function registerService() does not exist
My service provider looks like that:
<?php
namespace App\Providers;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
use App\Contracts\Extractor;
class ExtractorServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* #var bool
*/
protected $defer = true;
/**
* Available services for channels
*
* #var array
*/
protected $availableServices = ['AExtractor', 'ZExtractor'];
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
$this->app->call('registerService');
}
/**
* #param Request $request
* #return void
*/
protected function registerService(Request $request)
{
$tag = DB::table('channels')->where('id', $request->channel)->value('tag')->first()->tag;
$selectedExtractor = $tag . 'Extractor';
$extractor = 'AExtractor';
if(in_array($selectedExtractor, $this->availableServices)) {
$extractor = $selectedExtractor;
}
$this->app->bind('App\Contracts\Extractor', "App\\Helpers\\{$extractor}");
}
/**
* Get the services provided by the provider.
*
* #return array
*/
public function provides()
{
return [Extractor::class];
}
}
How can I use $this->app->call('registerService'); to call my registerService function and inject the Request object?
The problem is you're calling App:call in a wrong way: you have to specify the object on which you want to call the method and the method, like this :
$this->app->call( [ $this, 'registerService' ] );

"Unresolvable dependency resolving" Error, Laravel Service Provider

I'm trying my hand at building a custom Service Provider package, however I'm running into the following error. Does anyone have experience with this?
Unresolvable dependency resolving [Parameter #0 [ <required> $app ]] in class Illuminate\Support\ServiceProvider
Package Folder Structure:
[root]
....packages/
........mbarwick83/
............previewr/
................src/
....................PreviewrServiceProvider.php
....................Previewr.php
................composer.json
config/app.php:
Mbarwick83\Previewr\PreviewrServiceProvider::class
Service Provider:
<?php
namespace Mbarwick83\Previewr;
use Illuminate\Support\ServiceProvider;
class PreviewrServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* #var bool
*/
protected $defer = false;
/**
* Perform post-registration booting of services.
*
* #return void
*/
public function boot()
{
//
}
/**
* Register any package services.
*
* #return void
*/
public function register()
{
$this->app->bind('Mbarwick83\Previewr\Previewr',function($app){
return new Previewr($app);
});
}
}
Previewr.php (class):
<?php
namespace Mbarwick83\Previewr;
class Previewr
{
/**
* Create a new Previewr Instance
*/
public function __construct()
{
//
}
/**
* Friendly welcome
*
* #param string $phrase Phrase to return
*
* #return string Returns the phrase passed in
*/
public function something($phrase)
{
return $phrase;
}
}
Controller/view:
use Mbarwick83\Previewr\PreviewrServiceProvider as Previewr;
public function index(Previewr $previewr)
{
echo $previewr->something('Hello, League!');
}
composer.json:
"autoload": {
"psr-4": {
"Mbarwick83\\Previewr\\": "packages/Mbarwick83/Previewr/src"
}
},
In your controller, are you sure you want to inject your service provider?
use Mbarwick83\Previewr\PreviewrServiceProvider as Previewr;
Chances are you want to use this instead:
use Mbarwick83\Previewr\Previewr;

Laravel extend Form class

I'm trying to extend the Form class in L4.1 but I seem to be missing something. My file is named FormBuilder.php based on the API and is saved in app/libraries/extended/FormBuilder.php.
<?php namespace Extended;
class FormBuilder extends \Illuminate\Html\FormBuilder {
/**
* Create a text input field.
*
* #param string $name
* #param string $value
* #param array $options
* #return string
*/
public function text($name, $value = null, $options = array())
{
$options = $options + array('id'=>"field-{$name}");
return $this->input('text', $name, $value, $options);
}
}
This is actually the first time I've tried extending a core class in Laravel. I can't seem to put my finger on how to properly extend core classes like this Form class.
Edit:
I added "app/libraries/extended" to my composer.json file and ran both composer.phar update and composer.phar dump-autoload but it still seemed to be using the core class instead of my extended one. What am I forgetting to do?
To extend/swap a Laravel core class, you can create a Service Provider:
File: app/App/Libraries/Extensions/FormBuilder/FormBuilderServiceProvider.php
<?php namespace App\Libraries\Extensions\FormBuilder;
use Illuminate\Support\ServiceProvider as IlluminateServiceProvider;
use App\Libraries\Extensions\FormBuilder\FormBuilder;
class FormBuilderServiceProvider extends IlluminateServiceProvider {
/**
* Indicates if loading of the provider is deferred.
*
* #var bool
*/
protected $defer = true;
/**
* Register the service provider.
*
* #return void
*/
public function register()
{
$this->app->bindShared('formbuilder', function($app)
{
$form = new FormBuilder($app['html'], $app['url'], $app['session.store']->getToken());
return $form->setSessionStore($app['session.store']);
});
}
/**
* Get the services provided by the provider.
*
* #return array
*/
public function provides()
{
return array('formbuilder');
}
}
Create a Facade for it:
File: app/App/Libraries/Extensions/FormBuilder/FormBuilderFacade.php
<?php namespace App\Libraries\Extensions\FormBuilder;
use Illuminate\Support\Facades\Facade as IlluminateFacade;
class FormBuilderFacade extends IlluminateFacade {
/**
* Get the registered name of the component.
*
* #return string
*/
protected static function getFacadeAccessor() { return 'formbuilder'; }
}
This would be your namespaced service class:
File: app/App/Libraries/Extensions/FormBuilder/FormBuilder.php
<?php namespace App\Libraries\Extensions\FormBuilder;
use \Illuminate\Html\FormBuilder as IlluminateFormBuilder;
class FormBuilder extends IlluminateFormBuilder {
public function text($name, $value = null, $options = array())
{
$options = $options + array('id'=>"field-{$name}");
return $this->input('text', $name, $value, $options);
}
}
Open app/config/app.php and your Service Provider to the list
'App\Libraries\Extensions\FormBuilder\FormBuilderServiceProvider',
And replace Laravel's Form alias with yours
'Form' => 'App\Libraries\Extensions\FormBuilder\FormBuilderFacade',
To test I created a router like this:
Route::any('test', function() {
return e(Form::text('first_name'));
});
And it gave me this result:
<input id="field-first_name" name="first_name" type="text">

Categories