Custom validator in Laravel 5
I am upgrading my Laravel application from 4 to 5. However, I have a custom validator that I cannot get to work.
In L4, I made a validators.php file and included it in global.php using require app_path().'/validators.php';
.
I tried doing somewhat the same in L5. I dropped a validator in app/Validators/Validators.php, and updated my composer.json.
"files": [
"app/Validators/Validators.php"
]
However, now nothing renders on any page. What've I done wrong?
Try the following:
- Make a bind class where you can implement each rule you want extending
Validator
class. - Make a service provider that extends
ServiceProvider
. - Add your custom validator provider at
config/app.php
file.
You can create the bind at Services
folder like this:
namespace MyApp\Services;
class Validator extends \Illuminate\Validation\Validator{
public function validateFoo($attribute, $value, $parameters){
return $value == "foo"
}
}
Then, use a service provider to extends the core:
namespace MyApp\Providers;
use MyApp\Services\Validator;
use Illuminate\Support\ServiceProvider;
class ValidatorServiceProvider extends ServiceProvider{
public function boot()
{
\Validator::resolver(function($translator, $data, $rules, $messages)
{
return new Validator($translator, $data, $rules, $messages);
});
}
public function register()
{
}
}
Finally, import your service provider at config/app.php
like so:
'providers' => [
...
...
'MyApp\Providers\ValidatorServiceProvider';
]
so here's what I did on adding a custom validation. this is for laravel 5.1
- run
PHP Artisan make:request MyFormValidationRequest
file is created underapp\Requests\MyFormValidationRequest.php
Here's the initial code:
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class MyFormValidationRequest extends Request
{
/**
* 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()
{
return [
//
];
}
}
IMPORTANT: Change the return value of authorize()
method to true, if you're not doing any authentication. it's initial value is false. else you get a white page with a "Forbidden" error message.
-
I added a rule under the function
rules()
, here's what it looks likepublic function rules() { return [ 'activeuntil' => 'today_onwards' ]; }
today_onwards
is my new validation.
I created a folder named 'Services' under App folder
-
I created a file named 'ValidatorExtended.php' under App\Services folder , here's the code below:
<?php namespace App\Services; use Illuminate\Validation\Validator; use Carbon\Carbon; class ValidatorExtended extends Validator { private $_custom_messages = array( "today_onwards" => "The :attribute must be today onwards", ); public function __construct( $translator, $data, $rules, $messages = array(), $customAttributes = array() ) { parent::__construct( $translator, $data, $rules, $messages, $customAttributes ); $this->_set_custom_stuff(); } protected function _set_custom_stuff() { //setup our custom error messages $this->setCustomMessages( $this->_custom_messages ); } protected function validateTodayOnwards( $attribute, $value ) { $now = strtotime('-1 day'); $valueDateFormat = strtotime($value); if($valueDateFormat > $now){ return true; } else { return false; } } }
Note: the validateTodayOnwards method is where you put your logic. the name of the method should always start in "validate" then the name of your new validation key which should be in title case,
Another note your validation key should be separated by underscore and all small letters, in this case, "today_onwards". the underscore should be put before all first capital letters in the method name. I hope I explained it good.
TodayOnwards method is equivalent to validation name of "today_onwards",
another example, if I created validateOldPassword, your validation key should be "old_password".
-
I added below code in
app\Providers\AppServiceProvider.php
insideboot()
method.Validator::resolver(function($translator, $data, $rules, $messages = array(), $customAttributes = array()) { return new ValidatorExtended($translator, $data, $rules, $messages, $customAttributes); });
-
Don't forget to add below library, one is the Validator class and the other is your own class which is the "
ValidatorExtended
".use App\Services\ValidatorExtended; use Illuminate\Support\Facades\Validator;
-
Here's what the whole file looks like, [
app\Providers\AppServiceProvider.php
]<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use App\Services\ValidatorExtended; use Illuminate\Support\Facades\Validator; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { // Validator::resolver(function($translator, $data, $rules, $messages = array(), $customAttributes = array()) { return new ValidatorExtended($translator, $data, $rules, $messages, $customAttributes); }); } /** * Register any application services. * * @return void */ public function register() { // } }
That's it. done. you created your own custom validation.
-
Additionally, if you want to use it in your controller, below is the code:
class testController extends Controller { public function updatePass(MiscValidation $request){ //code here } }
Instead of using Request Class you use your own class which is an extension of the Request class.