How to wrire custom validation rule in controller Laravel? -


i have default validation rule in controller laravel:

$validator = validator::make($request->all(), [             'email' => 'required|email',             'phone' => 'required|numeric',             'code' => 'required|string|min:3|max:4',             'timezone' => 'required|numeric',             'country' => 'required|integer',             'agreement' => 'accepted'         ]); 

i tried this, dont know how transfer parameters inside function:

 public function boot()     {         validator::extend('phone_unique', function($attribute, $value, $parameters) {             return substr($value, 0, 3) == '+44';         });     } 

how can extent validation own rule? example need validate concatination of inputs:

$phone = $request->code.' '.$request->phone 

after check if $phone exists in database

i want use method:

>  $validator->sometimes('phone', 'required|alpha_dash|max:25', function > ($input) { >             if ((auth::user()->phone == $input->phone)) { >                 return false; >  >             } else { >  >                 $t = user::where("phone", $input->phone)->get(); >                 return ($t->count() > 0) ? false : false; >  >             } >         }); 

it not work under conditions (true, false) inside.

i added new validation nickname_unique:

 $validator = validator::make($request->all(), [             'email' => 'required|email',             'code' => 'required|string|min:3|max:4',             'phone' => 'required|phone_unique',             'timezone' => 'required|numeric',             'country' => 'required|integer',             'nickname' => 'required|alpha_dash|max:25',             'agreement' => 'accepted'         ], [             'phone_unique' => 'phone exists!',             'nickname_unique' => 'nickname busy!',         ]); 

it not work, not call validation rule below previos:

validator::extend('nickname_unique', function ($attribute, $value, $parameters, $validator) {              dd("here");  }); 

you can define custom validator inside appserviceprovider this:

<?php  namespace app\providers;  use illuminate\support\serviceprovider; use illuminate\support\facades\validator;  class appserviceprovider extends serviceprovider {     /**      * bootstrap application services.      *      * @return void      */     public function boot()     {         validator::extend('phone_unique', function ($attribute, $value, $parameters, $validator) {           $inputs = $validator->getdata();           $code = $inputs['code'];           $phone = $inputs['phone'];           $concatenated_number = $code . ' ' . $phone;           $except_id = (!empty($parameters)) ? head($parameters) : null;            $query = user::where('phone', $concatenated_number);           if(!empty($except_id)) {             $query->where('id', '<>', $except);           }            return $query->->exists();       });      /**      * register service provider.      *      * @return void      */     public function register()     {         //     } } 

you can inputs passed validator, accessing $validator property - getdata()

you can add parameter rules array after custom validation rule (just after colon) this:

'phone' => 'required|phone_unique:1', 

pass id ignored while checking entries db

the custom validator closure receives 4 arguments: name of $attribute being validated, $value of attribute, array of $parameters passed rule, , validator instance.

now can call validator this:

$validator = validator::make($request->all(), [       'email' => 'required|email',       'code' => 'required|string|min:3|max:4',       'phone' => 'required|phone_unique:1',       'timezone' => 'required|numeric',       'country' => 'required|integer',       'agreement' => 'accepted'   ], [     'phone_unique' => 'phone exists!', // <---- pass message custom validator   ]); 

see more custom validation rules.


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -