Laravel 5.8 Validation Race Condition
Asked Answered
C

2

10

I have this validation rule on POST request method inside a controller:

class CreateOrderController extends Controller
{
    public function create(Request $request)
    {
        $this->validate($request, [
            'store_id' => $request->order_type === OrdersTypeConstants::P2P  ? "" : "required|" . 'exists:stores,id',
            'p2p_type' => [Rule::in([OrdersTypeConstants::P2PCOURIER, OrdersTypeConstants::P2PPURCHASE])],
            'items' => 'required_if:p2p_type,'.OrdersTypeConstants::P2PPURCHASE.'|array',
            'items.*.id' => 'nullable|numeric',
            'items.*.quantity' => 'nullable|integer|min:1',
            'items.*.adjustment' => 'nullable|numeric',
            'items.*.image' => 'nullable|string',
            'items.*.addons' => 'array',
            'items.*.reward_applied' => 'boolean',
            'items.*.replacement_strategy.type' => [
                'string',
                Rule::in([ItemReplacementStrategyConstants::REMOVE,ItemReplacementStrategyConstants::BEST_MATCH, ItemReplacementStrategyConstants::SPECIFIC])
            ],
            'items.*.replacement_strategy.quantity' => 'integer|min:1',
            'items.*.replacement_strategy.item_id' => 'numeric',
            'address_id' => 'exists:addresses,id,user_id,' . $client_id,
            'address_id_p1' => 'exists:addresses,id,user_id,' . $client_id,
            'use_cash_deposit' => 'boolean',
        ]);

Sometime it returns The store id field is required even if it is actually being sent as you can see here in the error log: enter image description here

It is only happening randomly -not consistently- only on production environment, reported only on firebase.

Why could that be happening?

Chaparajos answered 8/9, 2020 at 8:35 Comment(10)
what inside your validate() ? i didnt see you using Request::validate() or Validator::make()Rammish
@AlzafanChristian Controllers have a validate method, the base Controller in your application uses the ValidatesRequest traitSynesthesia
@Synesthesia yeah sorry, totally forgot contorller in app/Http/Controllers/Controller has ValidatesRequests, i didnt always extends that ControllerRammish
thanks @Synesthesia I updated the code snippet context to show the classChaparajos
If you log $request->all() just before the validator, is the value present there? I can't see why it wouldn't be, but just to be sure.Zouave
it is only happening randomly -not consistently- only on production environment, reported only on firebase @ZouaveChaparajos
That is probably useful information to include within the original post.Zouave
I think $request->order_type may or may not exist when it runs the validation rule which explains the inconsistency. look into require if laravel.com/docs/8.x/validation#rule-required-ifElenor
As @Elenor pointed it seems to be related to order_type is present or not. Can you update the question and show if order_type is present and if is equals or not to OrdersTypeConstants::P2P?Mexican
Please don't make validation in the controller class, create the FormRequest file for following the SOLID principle. Code is terrible to watch & read.Glarus
Y
1

Happened to me several times. All the times the problem was an invalid JSON. You can validate yours here.

When Laravel encounters an invalid JSON, it just ignores it, and assumes the payload is empty.

The fact that it gives you a validation error on store_id is required is not a coincidence. Your framework is setup to stop on first failed rule, and this rule just happens to be store_id => 'required'.

Yezd answered 10/9, 2021 at 8:36 Comment(0)
T
0

Check if request()->all() has any data (your json could be invalid)

Tripinnate answered 30/12, 2021 at 8:59 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.