Tuesday, March 13, 2018

Pass custom parameters in laravel 5.4 validation - development

Pass custom parameters in laravel 5.4 validation

I am developing an application in laravel 5.4 and vuejs (multi lang support), which validate the content of a csv file. I have parsed csv file using vuejs and result json array post to Laravel.

On Laravel I looped the json array and validate the field using laravel validator.

$i = 0;
foreach ($json_array as $key => $value) {

   $params contains employee_id's and employee_name's

   $i = $i + 1;       

   Validator::make($params, [
            'employee_id'         => 'required|integer|exists:employees,employee_id',
            'employee_name'       => 'required|string',     
        ])->validate();
}

This is possible using laravel validation and laravel trigger error if any breaks in validation rule.

example: Laravel show error “employee_name is required” if the csv file have an empty field of employee_name.

Now I need to show error with line number (row), Line number can get using $i variable. How can I pass the $i variable (row) to validation file to show my error with line number.

I already spented lot of time to pass variable as parameter to validation message(on validation file). But i did’nt get a solution.

Output i expect is :

Example :

“employee_name is required in line : 8” if the csv file have an empty field of employee_name on line 8.

Thanks for reply.



from Laravel Questions and Answers https://laravelquestions.com/laravel/pass-custom-parameters-in-laravel-5-4-validation/
via Lzo Media

No comments:

Post a Comment