Conor
Conor

Reputation: 2563

Laravel validate array iterations

I sent an array from my form by javascript which gets values from different input fields and then storing it in a single variable.

<input type="hidden" name="details" id="details">

js

document.querySelectorAll('form')[1].addEventListener('submit', function(event) {
    var details = [];
    for (var i = 0; i < {{ $event->grocery->items()->count() }}; i++) {
        details[i] = [
            document.querySelectorAll('.store')[i].value,
            document.querySelectorAll('.item')[i].value,
            document.querySelectorAll('.quantity')[i].value,
            document.querySelectorAll('.brand')[i].value,
            document.querySelectorAll('.size')[i].value,
        ];
    }

    document.querySelector('#details').value = JSON.stringify(details);
});

Then in my controller, I decode the array using json_decode

$request->details = json_decode($request->details);

Now, I want to validate each iteration (just to check whether it is empty or not). So, I do like this,

$request->validate([
    ...
    'details.*.*' => 'required'
]);

But my problem is that this is not doing anything. Even if I sent an empty iteration it continues without returning an error.

Am I doing anything wrong here? Please help me.

Update

Example var dumping details

array:3 [▼
  0 => array:5 [▼
    0 => "Grocery"
    1 => "Grocery"
    2 => "Grocery"
    3 => "Grocery"
    4 => "Grocery"
  ]
  1 => array:5 [▼
    0 => "Grocery"
    1 => "Grocery"
    2 => "Grocery"
    3 => "Grocery"
    4 => "Grocery"
  ]
  2 => array:5 [▼
    0 => "Grocery"
    1 => "Grocery"
    2 => "Grocery"
    3 => "Grocery"
    4 => "Grocery"
  ]
]

Upvotes: 1

Views: 708

Answers (1)

Ramy Herrira
Ramy Herrira

Reputation: 624

It would make it easier if you named your array values

document.querySelectorAll('form')[1].addEventListener('submit', function(event) {
    var details = [];
    for (var i = 0; i < {{ $event->grocery->items()->count() }}; i++) {
        details[i] = {
            store: document.querySelectorAll('.store')[i].value,
            item: document.querySelectorAll('.item')[i].value,
            quantity: document.querySelectorAll('.quantity')[i].value,
            brand:document.querySelectorAll('.brand')[i].value,
            size: document.querySelectorAll('.size')[i].value,
        };
    }

    document.querySelector('#details').value = JSON.stringify(details);
});

The validation would look like this:

$request->validate([
    ...
    'details.*.store' => 'required',
    'details.*.item' => 'required',
    'details.*.quantity' => 'required',
    'details.*.brand' => 'required',
    'details.*.size' => 'required'
]);

But if it doesn't suit you there is always Laravel's Custom Validation Rules

EDIT:

I think maybe the issue is with the update of your data:

$request->details = json_decode($request->details);

Instead you should modify your input befor validation and Modify input before form request validation

$request->request->set('details', json_decode($request->details));

Upvotes: 1

Related Questions