Look at it.
Resolving Multiple Custom Validators?
So I have the need to create some custom validators in my Laravel build, so I've created them and then resolved them in a ValidationServiceProvider:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Validators\VideoValidator;
use App\Validators\ZipcodeValidator;
class ValidationServiceProvider extends ServiceProvider {
public function boot()
{
$this->app->validator->resolver(function($translator, $data, $rules, $messages)
{
return new VideoValidator($translator, $data, $rules, $messages);
});
$this->app->validator->resolver(function($translator, $data, $rules, $messages)
{
return new ZipcodeValidator($translator, $data, $rules, $messages);
});
}
public function register()
{
}
}
The problem is, the site now throws an error when I attempt to use both of them together. They each work without the other, but if I try to validate fields in a form using both of them I get an Exception.
My best guess is that I'm using the resolver method twice, and maybe it's only supposed to be used once? But it can only have 1 return, so how am I supposed to do that?
I've solved this before by using a single custom validator with multiple validation methods and using extend() rather than resolve(). My service provider:
public function boot()
{
/* snip */
$this->registerValidationRules($this->app['validator']);
}
protected function registerValidationRules(\Illuminate\Contracts\Validation\Factory $validator)
{
$validator->extend('zip', 'Gvt\Support\Validators\GvtRuleValidator@validateZip');
$validator->extend('state', 'Gvt\Support\Validators\GvtRuleValidator@validateStateCode');
$validator->extend('phone', 'Gvt\Support\Validators\GvtRuleValidator@validatePhone');
$validator->extend('county', 'Gvt\Support\Validators\GvtRuleValidator@validateCounty');
$validator->extend('party', 'Gvt\Support\Validators\GvtRuleValidator@validatePoliticalParty');
$validator->extend('ballot_style', 'Gvt\Support\Validators\GvtRuleValidator@validateBallotStyle');
}
Please or to participate in this conversation.