darobin/formic

Multiple "append" constructs

Closed this issue · 2 comments

First, I want to say that an input with such a name seems completely useless to me. That said, going along with the whole "don't lose any data" goal, how should we handle it?

<form enctype='application/json'>
  <input name='foo[][]' value='0'>
  <input name='foo[][]' value='1'>
  <input name='foo[][][]' value='2'>
  <input name='foo[1][]' value='3'> <!-- append/merge? -->
</form>

Based on my understanding of the spec, I'm guessing it would result in

{
    "foo":
    [
        [
            0
        ],
        [
            [
                1,
                3
            ]
        ],
        [
            [
                2
            ]
        ]
    ]
}

Is this correct?

No, it isn't. The appending construct [] only works as the last step. Having multiple appends seems pretty contrived to me (can you think of an actual use case?). Testing it with the implementation at http://darobin.github.io/formic/json/ confirms that (if you change the inputs).

You get:

{
    "foo[][]": [
        "0",
        "1"
    ],
    "foo[][][]": "2",
    "foo": [
        null,
        [
            "3"
        ]
    ]
}

(Note also that you only get actual numbers from <input type=number>, here you get strings.)

In the spec, this falls under step 8.1.3 of http://darobin.github.io/formic/specs/json/#dfn-steps-to-parse-a-json-encoding-path

@darobin No, I can't think of a possible use case, but thanks for the clarification. I'm the maintainer of a jquery plugin which supports form-to-object serialization and I'm pushing your spec forward with the plugin's next major version release.