1

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"
  ]
]
8
  • you do not specify which fields should be present for each array item. E.g. 'details.*.*.store' Commented May 9, 2018 at 8:33
  • @ThomasMoors Even if I do 'details.*.0', it doesn't care. Commented May 9, 2018 at 8:34
  • is 0 a field? No 'details.*.0'.quantity' could be required. That the field quantity should be present on every 0th item inside an array inside an array called details... Commented May 9, 2018 at 8:35
  • @ThomasMoors, I don't think so. If you take a look at the js code you can see that I am creating an associative array. Commented May 9, 2018 at 8:37
  • Can you show us what var_dump(json_decode($request->details)) displays? Commented May 9, 2018 at 8:38

1 Answer 1

1

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));
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry for the late response. But, still it's not working
I think it is an object rather than an array. Can you show me how to validate an object?
Convert it to associative array with json_decode($request->details, true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.