Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use sometimes rule in Laravel 5 request class

Tags:

I have the following request class:

<?php namespace App\Http\Requests\User;  use App\Http\Requests\Request; use Validator; use Session; use Auth; use App\User;  class RegisterStep1Request extends Request {      /**      * Determine if the user is authorized to make this request.      *      * @return bool      */     public function authorize()     {         return true;     }      /**      * Set up the validation rules      */     public function rules()     {         Validator::extend('valid_date', function($attribute, $value, $parameters)         {             $pieces = explode('/', $value);             if(strpos($value, '/')===FALSE) {                 return false;             } else {                 if(checkdate($pieces[1], $pieces[0], $pieces[2])) {                     return true;                 } else {                     return false;                 }             }         });          return [             'first_name' => 'required',             'last_name' => 'required',             'email' => 'required|email|unique:users,email',             'dob' => 'required|regex:/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/|valid_date',             'mobile' => 'required',             'password' => 'required|confirmed'         ];     }      public function messages()     {         return [             'first_name.required' => 'The first name field is required.',             'last_name.required' => 'The last name field is required.',             'email.required' => 'The email address field is required.',             'email.email' => 'The email address specified is not a valid email address.',             'email.unique' => 'The email address is already registered with this website.',             'dob.required' => 'The date of birth field is required.',             'dob.regex' => 'The date of birth is invalid. Please use the following format: DD/MM/YYYY.',             'dob.valid_date' => 'The date of birth is invalid. Please check and try again.',             'mobile.required' => 'The mobile number field is required.',             'password.required' => 'The password field is required.',             'password.confirmed' => 'The confirm password field does not match the password field.'         ];     }  } 

I want to add the following sometimes rule:

Validator::sometimes('dob', 'valid_date', function($input) {     return apply_regex($input->dob) === true; }); 

How would I add this to my request class?

I have amended my rules method to the following:

public function rules() {     Validator::extend('valid_date', function($attribute, $value, $parameters)     {         $pieces = explode('/', $value);         if(strpos($value, '/')===FALSE) {             return false;         } else {             if(checkdate($pieces[1], $pieces[0], $pieces[2])) {                 return true;             } else {                 return false;             }         }     });      Validator::sometimes('dob', 'valid_date', function($input)     {         return apply_regex($input->dob) === true;     });      return [         'first_name' => 'required',         'last_name' => 'required',         'email' => 'required|email|unique:users,email',         'dob' => 'sometimes|required|regex:/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/|valid_date',         'mobile' => 'required',         'password' => 'required|confirmed'     ]; } 

But I now get the following error when I submit the form:

FatalErrorException in Facade.php line 216: Call to undefined method Illuminate\Validation\Factory::sometimes() 
like image 242
geoffs3310 Avatar asked May 18 '15 09:05

geoffs3310


People also ask

What is sometimes validation in laravel?

sometimes adds the defined validation conditions to a given field if the field is present in the request. It's therefore useful in situations where you wish to run validation checks against a field only if that field is present in the request.

How do you add a validation rule in laravel?

You should add all your validation logic in the passes() function. It should return true or false based on the logic you have written in the function. The message() function returns a string that specifies the error message to be displayed in case the validation fails.

What is request -> input () in laravel?

input() is a method of the Laravel Request class that is extending Symfony Request class, and it supports dot notation to access nested data (like $name = $request->input('products.0.name') ).

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

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

There is a documented way to make changes to the request's validator instance in Laravel 5.4. You should implement the withValidator method for that.

Based on the example from @lukasgeiter's answer, you may add the following to your request class:

/**  * Configure the validator instance.  *  * @param  \Illuminate\Validation\Validator  $validator  * @return void  */ public function withValidator($validator) {     $validator->sometimes('dob', 'valid_date', function ($input) {         return apply_regex($input->dob) === true;     }); } 

By doing this you don't have to worry about overriding internal methods. Besides, this seems to be the official way for configuring the validator.

like image 119
Gustavo Straube Avatar answered Nov 04 '22 10:11

Gustavo Straube


You can attach a sometimes() rule by overriding the getValidatorInstance() function in your form request:

protected function getValidatorInstance(){     $validator = parent::getValidatorInstance();      $validator->sometimes('dob', 'valid_date', function($input)     {         return apply_regex($input->dob) === true;     });      return $validator; } 
like image 35
lukasgeiter Avatar answered Nov 04 '22 09:11

lukasgeiter