ohffs's avatar
Level 50

submitForm with multidimensional array

I was writing a test for a bit of code which has fields which are arrays - some of those fields values are also arrays too. The code works fine in the browser, but when I try and write a test for it I think the framework is reading the array values as key => values rather than a flat array. So :

$newData = [
            'name' => [ $photo->id => 'QPQPQPQP' ],
            'galleries' => [ $photo->id => [$gallery->id, $gallery2->id] ]
];
...
$this->actingAs($user)
  ...
  ->submitForm($newData)

The submitForm errors out with :

InvalidArgumentException: Unreachable field "0"

A var_dump of $newData shows :

array(2) {
  'name' =>
  array(1) {
    [447] =>
    string(8) "QPQPQPQP"
  }
  'galleries' =>
  array(1) {
    [447] =>
    array(2) {
      [0] =>
      int(88)
      [1] =>
      int(89)
    }
  }
}

Which is why I'm wondering if the 'unreachable field 0' is actually the key for the galleries values array. If I comment out the galleries field in $newData it goes past the submitForm line so it's definitely not happy with it. Just not sure if I'm doing something wrong, or the framework just doesn't like this kind of thing?

0 likes
1 reply
ohffs's avatar
Level 50

Just to update - I discovered that the old multi-dimensional work-arounds have been fixed in the framework, so you can just do :

$newData = [
            'name' => [ $photo->id => 'QPQPQPQP' ],
            "galleries[{$photo->id}]" => [$gallery->id, $gallery2->id]
];

Please or to participate in this conversation.