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?

Muhammad Sultan
  • 473
  • 5
  • 19
  • 1
    what inside your validate() ? i didnt see you using Request::validate() or Validator::make() – Alzafan Christian Sep 08 '20 at 08:41
  • 1
    @AlzafanChristian Controllers have a `validate` method, the base Controller in your application uses the `ValidatesRequest` trait – lagbox Sep 08 '20 at 08:44
  • @lagbox yeah sorry, totally forgot contorller in app/Http/Controllers/Controller has ValidatesRequests, i didnt always extends that Controller – Alzafan Christian Sep 08 '20 at 08:50
  • thanks @lagbox I updated the code snippet context to show the class – Muhammad Sultan Sep 08 '20 at 08:51
  • 1
    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. – PatricNox Sep 08 '20 at 09:46
  • it is only happening randomly -not consistently- only on production environment, reported only on firebase @PatricNox – Muhammad Sultan Sep 08 '20 at 11:24
  • 1
    That is probably useful information to include within the original post. – PatricNox Sep 08 '20 at 12:30
  • I think $request->order_type may or may not exist when it runs the validation rule which explains the inconsistency. look into require if https://laravel.com/docs/8.x/validation#rule-required-if – Cameron Nov 12 '20 at 01:45
  • As @Cameron 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`? – Vinícius Fagundes Jun 19 '21 at 17:35
  • Please don't make validation in the controller class, create the FormRequest file for following the SOLID principle. Code is terrible to watch & read. – Василий Пивоваров Aug 21 '21 at 23:15

2 Answers2

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'.

Victor Timoftii
  • 2,717
  • 1
  • 16
  • 15
0

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

AID
  • 124
  • 5