Reputation: 143
How to validate recursive nested objects in Laravel 5+
I have structure of balance of the company. There is a pattern
<row>: {
name
digit
rows: [<row>]
}
The example of the json structure
{
"balance_data": {
"rows": [
{
"name": "aktiva",
"digit": "5555.33",
"rows": [
{
"name": "balance catalog 1",
"digit": "1234.12",
"rows": [
{
"name": "balance subcatalog name 1",
"digit": "4321.21",
"rows": []
},
{
"name": "balance subcatalog name 2",
"digit": "4321.21",
"rows": []
}
]
},
{
"name": "balance catalog 2",
"digit": "1234.12",
"rows": [
{
"name": "balance subcatalog name 3",
"digit": "4321.21",
"rows": []
}
]
}
]
}
]
}
}
How i can validate digit as required in all nested and subnested objects?
Upvotes: 1
Views: 895
Reputation: 1518
<?php
function myFunction($value, $key)
{
// do validation, this will be called for every key, recursively
}
$arr=array("balance_data"=>array("row"=>"", array("row"=>"2")));
array_walk_recursive($arr, "myFunction");
?>
This can be achieved using inbuild array_walk_recursive
function of php
Upvotes: 1
Reputation: 3537
You would have to write a custom validator for that. Check out this section of the docs here.
Upvotes: 1