when using post data as a simple object i can use prepareForValidation works as a charm, but when using array of objects like demonstrated above i don't undestdand how to do.
@sys auditing Dude, you don’t need the prepareForValidation method! I’ve been telling you this for two days now on the Laravel Discord.
You have request data coming in. Just validate it. You don’t need to manipulate it or merge it. Again, stop trying to shoe-horn in usages of framework features that you don’t need. If you want to validate the incoming account IDs, then do so:
public function rules()
{
return [
'*.account_id' => 'exists:accounts,id',
];
}
I don't have the issue on validation side i did the same as your unswer, but as you can see my frontend post a data (account_id) via an API, this account_id posted as name (client knows only names) and i would like to save data as ID that why i'm using this statement :
$account = Account::select('id')->where('name', $account_id)->first();
In order to do that i'm preparing data before validation.
account_id posted as name (client knows only names) and i would like to save data as ID
@sys auditing Then that’s nothing to do with validation. Validate that the names the front-end is posting are existing accounts:
public function rules()
{
return [
'*.account_id' => ['required', 'string', 'exists:accounts,name'],
];
}
Then convert those names to IDs in your controller:
public function someAction(SomeFormRequest $request)
{
$names = Arr::pluck($accounts, 'account_id');
$accounts = Account::whereIn('name', $names)->get();
// Now do something with your retrieved account models here...
}