Search code examples
laravellaravel-4laravel-validation

Using a php variable for validation in Laravel 4.2


I have this code that gets an object in a session and i put it in a certain variable. Then i wanted to use it in the laravel 4.2 'same' validation. So far here is the code i have

        session_start();
        $getsCapt = $_SESSION["captcha"];

        $rules = array(
            'st'           => 'required',
            'capt'         => 'required|numeric|same:$getsCapt'
        );

It is not doing anything. What i wanted to have is i'll compare the value i get from the session with the value i get from the textbox in my view named 'capt' but so far its not doing anything. any ideas to do this properly?


Solution

  • First of all, You are using same validator incorrectly.

    same expects a form field name

    Example:

    same:field_name

    Where, the given field must match the field under validation.

    You could register and use a Custom Validation Rule

    Validator::extend('captcha', function($attribute, $value, $parameters)
    {
         $captcha = \Session::get('captcha');
         return $value == $captcha;
    });
    

    So later you can do:

        //session_start(); - Please no need for this in Laravel
        //$getsCapt = $_SESSION["captcha"]; - Also remove this not necessary
    
        $rules = array(
            'st'           => 'required',
            'capt'         => 'required|numeric|captcha'
        );
    

    NB: Use Session::put to save something to session e.g \Session::put('something'); There is also Session::get for retrieving value from session e.g \Session::get('something');

    Please avoid using $_SESSION not Laravel way of doing things

    [Edited] Where to Register Custom Validation Rule?

    There are basically two ways you can register a custom validation rule in Laravel.

    1. Resolving from a closure: If you are resolving through closure you can add it to : app/start/global.php

    Validator::extend('captcha', function($attribute, $value, $parameters)
    {
         $captcha = \Session::get('captcha');
         return $value == $captcha;
    });
    

    2. Resolving from a class This is the best and preferred way of extending custom validation rule as its more organised and easier to maintain.

    i. Create your own validation class, CustomValidator.php, maybe in app/validation folder

    <?php namespace App\Validation;
    
    use Illuminate\Validation\Validator;
    use Session;
    
    class CustomValidator extends Validator{
    
        public function validateCaptcha($attribute, $value, $parameters)
        {
            $captcha = Session::get('captcha');
            return $value == $captcha;
        }
    }
    

    NB: Notice the prefix validate used in the method name, validateCaptcha

    ii. Create a Service Provider that will resolve custom validator extension in app/validation folder

    <?php namespace App\Validation;
    
    use Illuminate\Support\ServiceProvider;
    
    class CustomValidationServiceProvider extends ServiceProvider {
    
    public function register(){}
    
       public function boot()
       {
            $this->app->validator->resolver(function($translator, $data, $rules, $messages){
                return new CustomValidator($translator, $data, $rules, $messages);
           });
       }
    
    }
    

    iii. Then add CustomValidationServiceProvider under app/config/app.php providers array:

    'providers' => array(
      <!-- ... -->
      'App\Validation\CustomValidationServiceProvider'
    ),
    

    iv. And add the custom error message in app/lang/en/validation.php

    return array(
    ...
    "captcha" => "Invalid :attribute entered.",
    ...
    )