Laravel Custom Validation: Show field data of failing record

Viewed 95

I need a way to place data of failing record along with count of records that failed because of the same reason. I hope the explanation is enough to get the requirement.

eg:

$rules['inventories.*.activity_id']= [ 'required', 'exists:activities,id ];

$messages['inventories.*.activity_id.required'] = 'Activity id can not be blank.'; $messages['inventories.*.activity_id.exists'] = "Activity id <<< [FAILING RECORD -> ACTIVITY_ID] >>> does not exist in the system. <<< NUMBER OF RECORDS FAILED FOR THIS REASON >>> entries skipped.";

Anyone have an idea about this? I found I can place :attributes but this does't not what I want.

2 Answers

You could try using the :input attribute to display the value you are checking the existence of in your custom validation message:

'Activity id :input does not exist in the system.'

Though this will not get you to your failure count part.

this shows you number of validation errors:

count($errors) 

if you want to show the invalid value that passed among request in the error message, you can do this:

public function messages()
{
    return [
        'fieldname.numeric' => 
                     'The :attribute must be numeric. Your value is '.$request->input('fieldname')
    ];
 }
Related