Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 236
Maeh Avatar asked Feb 09 '15 19:02

Maeh


People also ask

How do I create a custom validation rule in laravel?

Custom Validation Rule Using Closures $validator = Validator::make($request->post(),[ 'birth_year'=>[ 'required', function($attribute, $value, $fail){ if($value >= 1990 && $value <= date('Y')){ $fail("The :attribute must be between 1990 to ". date('Y').". "); } } ] ]);

What is custom validation laravel?

Custom Validation Rule Using ClosuresThe function in it is getting 3 values: attribute, value, and fail. The attribute is the field for which the validation is happening. The value corresponds to the actual value of the said object and failure is the callback method that would be executed once the validation fails.

How many types of validation are there in laravel?

Each form request generated by Laravel has two methods: authorize and rules .

What is the method used to configure validation rules in form request?

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.


2 Answers

so here's what I did on adding a custom validation. this is for laravel 5.1

  1. run PHP Artisan make:request MyFormValidationRequest file is created under app\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.


  1. I added a rule under the function rules(), here's what it looks like

    public function rules() {
        return [
            'activeuntil' => 'today_onwards'
        ];
    }
    

today_onwards is my new validation.

  1. I created a folder named 'Services' under App folder

  2. 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".

  1. I added below code in app\Providers\AppServiceProvider.php inside boot() method.

    Validator::resolver(function($translator, $data, $rules, $messages = array(), $customAttributes = array())
    {
        return new ValidatorExtended($translator, $data, $rules, $messages, $customAttributes);
    });
    
  2. 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;
    
  3. 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()
             {
                //
            }
        } 
    
  4. That's it. done. you created your own custom validation.

  5. 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.

like image 121
Rabb-bit Avatar answered Oct 30 '22 20:10

Rabb-bit


Try the following:

  1. Make a bind class where you can implement each rule you want extending Validator class.
  2. Make a service provider that extends ServiceProvider.
  3. 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';
]
like image 28
manix Avatar answered Oct 30 '22 22:10

manix