Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form helper does not handle numbers for nested fields #110

Closed
ghost opened this issue Sep 12, 2017 · 3 comments
Closed

Form helper does not handle numbers for nested fields #110

ghost opened this issue Sep 12, 2017 · 3 comments
Assignees
Labels

Comments

@ghost
Copy link

ghost commented Sep 12, 2017

Hi,

I ran into a small problem with version 1.0.0. I'm not sure whether it is a bug or me trying to do something I shouldn't. I came up with the following MWE:

require 'hanami-helpers'

include Hanami::Helpers

users = [ { user_id: 10 }, { user_id: 11 } ]

def params; {}; end

r = html.div do
  form_for :users, '#' do
    users.each_with_index do |a, i|
      fields_for i do
        hidden_field :user_id, value: a[:user_id]
        text_area :story
      end
    end
  end
end

puts r.to_s

It produces the following HTML code:

<div>
<form action="#" method="POST" accept-charset="utf-8" id="users-form">
<input type="hidden" name="users[][user_id]" id="users-0-user-id" value="10">
<textarea name="users[0][story]" id="users-0-story"></textarea>
<input type="hidden" name="users[][user_id]" id="users-1-user-id" value="11">
<textarea name="users[1][story]" id="users-1-story"></textarea>
</form>
</div>

As you can see, the name of the input type="hidden" is incomplete, and Rack raises an exception when the form is submitted. I thought that maybe using an Integer caused a problem somehow, so I replaced fields_for i do by fields_for i.to_s do but the result is the same. Only if I append or prepend a non-numeric character do I get to see the expected result.

@mereghost
Copy link
Member

Hi @White-Monkey , sorry for the delay in checking this. Could you check if the same happens with other kinds of inputs (text_field tel_field) too?

@mereghost mereghost self-assigned this Sep 27, 2017
@TiteiKo
Copy link
Contributor

TiteiKo commented Sep 27, 2017

Hi,

Because we're supporting fields_for_collection, we have a side effect that is that you can't have a subfield named by a numeric key (so user[1], user[1][foo] or just 1). Most of the time, if you think about doing something like that, you in fact want to have an array of items in the end

# so not this
{
  users: {
    "0": { user_id: "10", story: "..." },
    "1": { user_id: "11", story: "..." }
  }
}

# but this
{
  users: [
    { user_id: "10", story: "..." },
    { user_id: "11", story: "..." }
  ]
}

To have your action parameters look like this:

params do
  required(:users).each do
    schema do
      required(:user_id) { int? }
      required(:story)     { str? }
    end
  end
end

In a nutshell: this is an unavoidable side effect (in the current architecture of the code) of the really cool feature fields_for_collection which you should definitively be using in your example and that would solve your problem :)

If you want to avoid it, you just need to use an alphanumeric key (ie: n0 instead of 0)

@TiteiKo
Copy link
Contributor

TiteiKo commented Sep 28, 2017

@White-Monkey I'm closing in favor of #111

@TiteiKo TiteiKo closed this as completed Sep 28, 2017
@TiteiKo TiteiKo added wontfix and removed bug labels Sep 28, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants