Reputation: 4682
I am using a custom validation rule to validate some data that I read from a file. The rule is as follows :
$this->rules = [
'file_data' => 'required|array',
'file_data.*.id' => 'required',
'file_data.*.name' => 'required',
'file_data.*.code' => 'required|code_exists'
];
and apply these rules like
$validationResult = validator($data, $this->rules, $customMessageArray);
In the code_exists
, I need the value of id
for running a validation. I am trying to pass the value through $parameters
, like code_exists:id
, but it is giving me the string id
in the parameter array. How can I pass the actual value ?
Upvotes: 2
Views: 4466
Reputation: 973
You should get key of array and after get the id. Like that:
'file_data.*.code' => [
'required',
function($attribute, $value, $fail) use ($data) {
$index = explode('.', $attribute)[1];
$id = data_get($data, "file_data.$index.id");
$isExists = DB::table('table_name')->whereId($id)->whereCode($value)->exists();
if (!$isExists) {
return $fail(__('validation.exists', ['attribute' => $attribute]));
}
}
],
Upvotes: 4