Receive data from input_form as lists

Hey guys, there is a way to receive multiple data comming from a form, not like this:

  "documents" => %{
      "0" => %{
        "document" => "123456",
        "id" => "046b6bcc-5f91-475c-8e6a-b929b1aad11e",
        "issue_date" => "2020-12-15",
        "issuer" => "SSCSP",
        "type" => "RG"
      },
      "1" => %{
        "document" => "123456",
        "id" => "046b6bcc-5f91-475c-8e6a-b929b1aad11e",
        "issue_date" => "2020-12-15",
        "issuer" => "SSCSP",
        "type" => "RG"
      }
    }

and receive a list, like this?

    "documents" => [
      %{
        "document" => "123456",
        "id" => "046b6bcc-5f91-475c-8e6a-b929b1aad11e",
        "issue_date" => "2020-12-15",
        "issuer" => "SSCSP",
        "type" => "RG"
      },
      %{
        "document" => "123456",
        "id" => "046b6bcc-5f91-475c-8e6a-b929b1aad11e",
        "issue_date" => "2020-12-15",
        "issuer" => "SSCSP",
        "type" => "RG"
      }
    ]

I thought that there was a conf about that :thinking:

It is possible to receive arrays like this - in a form-urlencoded stream, that would look like foo[bar][]=1&foo[bar][]=2&foo[bar][]=17 and parse to:

%{
  "foo" => %{
    "bar" => ["1", "2", "17"]
  }
}

BUT this doesn’t work well if you want structured data past the []. Should foo[][bar]=1&foo[][baz]=2 parse as :

%{
  "foo" => [
    %{"bar" => "1"},
    %{"baz" => "2"},
  }
}

or

%{
  "foo" => [
    %{
      "bar" => "1",
      "baz" => "2"
    }
  ]
}

The former would be expected if there were two rows to enter records, one field filled out in each; the latter if there was one row and both were filled. The request parser can’t know which one is intended.

That’s the reason for the "0" and "1" keys: they unambiguously indicate which fields go together.

1 Like