You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm searching for a solution to validate each row for a excel import twice, each time with different dynamic rules, including the SkipsFailures trait.
In a input-controller manual validation scenario you can do this something like :
$validator1 = Validator::make($inputData,$rules1);
// store the error messages
// create new rules in $rule2 based on validated data $validated1 = $validator->validated();
// validate the data again
$validator2 = Validator::make($validated1,$rules2);
// rinse and repeat
In the import row validation you can pass the validation rules with public function rules(): array but I don't see a method that can be used to change those rules at row level and use them to validate the same row again.
A theoretical example could be:
rowId
model_id
some_field
1
55
Content Cell
2
66
Wrong Content Cell
3
555a
Content Cell
For each row, in the first validation step, you want to check the model_id column, if it's not integer the record will be skipped because of SkipsFailures trait.
If passes, in the second validation step you want to retrieve that Model first. Based on the retrieved model you want to generate/change a new set of rules to validate the some_field. If the second validation fails that record will be skipped too.
In the end, all the errors will be collected from $failures = $e->failures(); and will contain the validation errors from row 2 ( because has a wrong content ) and row 3 ( because the model_id is a string ). Row 1 will pass.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello,
I'm searching for a solution to validate each row for a excel import twice, each time with different dynamic rules, including the SkipsFailures trait.
In a input-controller manual validation scenario you can do this something like :
In the import row validation you can pass the validation rules with
public function rules(): array
but I don't see a method that can be used to change those rules at row level and use them to validate the same row again.A theoretical example could be:
For each row, in the first validation step, you want to check the
model_id
column, if it's not integer the record will be skipped because of SkipsFailures trait.If passes, in the second validation step you want to retrieve that Model first. Based on the retrieved model you want to generate/change a new set of rules to validate the
some_field
. If the second validation fails that record will be skipped too.In the end, all the errors will be collected from
$failures = $e->failures();
and will contain the validation errors from row 2 ( because has a wrong content ) and row 3 ( because the model_id is a string ). Row 1 will pass.Hope it was clear enough.
Thank you
Beta Was this translation helpful? Give feedback.
All reactions