#update api using form always return validation error

7 messages · Page 1 of 1 (latest)

finite meadow
#

Routes:
Route::apiResource('whitelist-ips', WhitelistIpController::class)->except('show');

Controllers:

public function update(UpdateRequest $request, WhitelistIp $whitelistIp): JsonResource
{
    $whitelistIp->update($request->validated());

    return new WhitelistIpResource($whitelistIp);
}

Update Request:

return [
    'ip_address' => ['required', 'ip', Rule::unique(WhitelistIp::class)->ignore($this->whitelist_ip)],
    'is_active' => ['required', 'boolean'],
];

Errors is in image, and what will be the reason for this? If I passes JSON and it can update.. but not form.
Any help is appreciated, thanks in advance

willow kiln
#

The syntax for Rule::unique() isn't right. Give it two arguments: table name and column name. And the ignore call expects an id, not a value.

finite meadow
#

And on my Original question, I read more about method spoofing, and yeah it makes sense... so apply that one for now

willow kiln
#

Try using debugbar to see what query your validation ends up running. I misunderstood the value you pass to ignore () but I'm 99.9% sure you can't just pass a classname to unique(). At least I've never seen anyone do it before, and it's not in the documentation.

finite meadow
#

Yeah about original question it's because of form data only (need to request as POST not PUT and spoofing).. now fixed.
and about Model to unique() it's working fine.. so maybe one of the options 🤣

willow kiln
#

Just tried it, and I was indeed wrong 🤣 Still I would advice against relying on undocumented features. I've done it myself in the past, without realizing it, and then a minor Laravel release broke everything. Experiences like that teach you to paint inside the box unless you have a very good reason not to 😆