Hello there, I still need some help, couldn't figure it out :(
[L5] Troubleshooting my custom validation rule
Hello there,
I'm having trouble trying to create a new validation rule. I want to use my rule like this :
// app/http/Controllers/ProductsController.php
$validation = Validator::make($data, ['field'=>'required|third-party-code']);
if ($validation->fails()) { /*...*/ }
So I registered a new service provider :
// app/Providers/AppServiceProvider
public function register()
{
$this->app->register(CustomValidationServiceProvider::class);
}
In my service provider I binded my validation class and registered the validation rule by extension :
namespace App\Providers;
use App\Validators\ProductValidator;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
class CustomValidationServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind('ProductValidator', function($app){
return $app->make(ProductValidator::class);
});
}
public function boot()
{
$this->registerValidationRules($this->app['validator']);
}
protected function registerValidationRules()
{
Validator::extend('third-party-code', 'ProductValidator@validateThirdPartyCode', 'third party code error message');
}
}
Finally the validator class :
namespace App\Validators;
class ProductValidator
{
public function validateThirdPartyCode($attribute, $value, $parameters)
{
return $value == 'some value' || $this->thirdPartyExists($value);
}
private function thirdPartyExists($thirdPartyCode)
{
// fake test for now
return !empty($thirdPartyCode);
}
}
When trying to use this new rule as described above, I'm getting the following error :
BadMethodCallException in Validator.php line 3360: Method [validateThirdPartyCode] does not exist.
The end of the call stack :
1. in Validator.php line 3360
2. at Validator->__call('validateThirdPartyCode', array('third_party_code', ' THIRD_PARTY_CODE', array(), object(Validator))) in Validator.php line 517
3. at Validator->validateThirdPartyCode('third_party_code', ' THIRD_PARTY_CODE', array(), object(Validator)) in Validator.php line 517
4. at Validator->validateAttribute('third_party_code', 'third-party-code') in Validator.php line 431
Looks like validateThirdPartyCode() is not called in my ProductValidator class, but in Validator class instead...
As you can see I'm using some code I found in other discussions about the same subject. But I need this extra little help to understand what's wrong here... Any help will be appreciated :)
EDIT : I tried using a closure :
Validator::extend('third-party-code', function($attribute, $value, $parameters)
{
return $value == 'foo';
});
I got the same "BadMethodCallException", so I think my "extend" is ignored. :/
Ok I understood why my custom validation was not called. Looks like when you use dashes or underscores in the validation keyword ("third-party-code"), the validation extensions are ignored and validateThirdPartyCode() is called on Validator, no matter how you extended the validation rules. Changing the keword to "thirdpartycode" somehow fixed my problem. I'm not sure if this is a bug or a feature tho...
Edit : it's actually a feature. Underscores work fine too. I just had to fix some namespaces issues (my code samples are edited above accordingly). GitHub issue here
Please or to participate in this conversation.