Skip to content

WireInputData should allow multidimensional arrays #387

@abdusco

Description

@abdusco

Short description of the issue

I'm building an inputfield for a custom fieldtype. The inputfield will display a (possibly) long list of items with a number of fields in each. Instead of setting input name attributes beforehand and posting all field data back and forth and filtering saveable data on the backend, I'm trying to reduce the amount of processing by setting input names using JS only if it has been changed.

// using vue.js
<input @change="changed($event, item, 'author')" type="text" v-model="item.author">
changed(event, item, key) {
    input = event.target;
    if (!input.name) input.name = `changed[${item.id}][${key}]`; // name as multidimensional array
}

When the page is submitted, this was supposed to give me an array like this
image

But after some head scratching I found out that WireInputData class actually removes multidimensional arrays from $_POST (and $_GET) in cleanArray() method

/**
 * Clean an array of data
 * Removes multi-dimensional arrays and slashes (if applicable) 
 * @param array $a
 * @return array
 */
protected function cleanArray(array $a) {
    $clean = array();
    foreach($a as $key => $value) {
        if(is_array($value)) continue; // we only allow one dimensional arrays
        if(is_string($value) && $this->stripSlashes) $value = stripslashes($value);
        $clean[$key] = $value;
    }
    return $clean;
}

Expected behavior

$input->post should allow multidimensional arrays

Actual behavior

It removes them

Optional: Suggestion for a possible fix

Removing the following line solves the issue

if(is_array($value)) continue; // we only allow one dimensional arrays

Steps to reproduce the issue

Create a set of inputs like this

<input name="changed[1][text]"/> // defaults to type=text
<input name="changed[1][status]"/>
<input name="changed[2][status]"/>

Post the form, $input->post->changed will be an empty array.

Setup/Environment

PW 3.0.76

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions