farshadf's avatar

overwriting the validation field (nested array) in laravel validation

i want to prepare a data for validatioon as laravel documenation says like below :

            $this->merge(['sender.info.code' => $$code, 'receiver' => $somefield]);

now what is the problem is for that nested array sender.info.code the code above dont merge the $code in that field which means that it cant find it . but the receiver field is being merged without problem . and here is how i validate them that valdiation is working fine too (i mean can accec the sender info code ) :

                reciver => "required",
                "sender.info.code" => "required|digits:4|" . Rule::in(somefunction),

now do you guys have any idea how can i merge this sender.infocodeinmerge` function

0 likes
4 replies
itsfg's avatar
itsfg
Best Answer
Level 5

Try merging it with a regular array :

$this->merge(['sender' => ['info' => ['code' => $$code]], 'receiver' => $somefield]);

PS : you have double $ on your $code. Just in case it is a typo...

1 like
farshadf's avatar

thanks its working but its overriding every key in that layer of array as i am testing am i right or its just replacing the code with the value ? because now i get the error that name is required though its there but in the same level with code in json nested

itsfg's avatar

Indeed, if you have other values in sender or sender.info, maybe you should get the full request array with $request->all(), add what you want to this array, and then merge it back.

farshadf's avatar

isnt there any way just to replace that keys and do not use all others key to validate because i dont want to touch others i just want to change this keys

Please or to participate in this conversation.