afulki
June 15, 2017, 1:24pm
1
I have a set of checkboxes on my form, they are not part of a change set,
How should I render them, currently I iterate over the list and create an input and label, they render correctly.
How do I retrieve their checked state in my update function of my controller?
I’ve tried every way I can think of, but seem to be missing something as they are not in the params.
Thank you
1 Like
Are they still inside of the form? If so then when that form is submitted they should by in the params inside that form name.
afulki
June 15, 2017, 4:51pm
3
Unfortunately not, here is a snipped of my form
<div class="row">
<%= form_for @changeset, @action, [class: "col s12"], fn f -> %>
<div class="row">
<div class="input-field col s12">
<%= for tag <- @tags do %>
<input type="checkbox" class="form-control filled-in" id="type[<%= tag.name %>-box]" <%= is_tagged(tag.name, @my_type.tags) %> value="true" />
<label for="type[<%= tag.name %>-box]"><%= tag.name %></label>
<% end %>
</div>
</div>
This is the params I receive in update:
%{"_csrf_token" => "Bj8QJzETEUAKRCg8fk8bH2ERGzUCNgAA5ReTrCIpAsII18yMVzvPMA==",
"_method" => "put", "_utf8" => "✓", "action" => "", "id" => "15",
"type" => %{"description" => "This is a new test type with enumeration",
"enums" => %{"0" => %{"delete" => "false",
"id" => "17fad99f-0812-45f0-8eb2-401f2ea56068", "mapped_value" => "One",
"source_value" => "1"},
"1" => %{"delete" => "false",
"id" => "1d605c5b-ef4a-4917-a9bf-f99050e71a30", "mapped_value" => "Two",
"source_value" => "2"}}, "is_enum" => "true", "name" => "TestType"}}
I’m probably doing something stupid, I have adjusted the name from something simple to names matching the way phoenix generates them with no success.
Can you show the generated HTML source for the entire form as well?
your input is missing a name
attribute. like this, the value won’t be included in the form submission. add one and it should work. this will also be the key in the params
map.
4 Likes
Hah I missed that, I’m used to calling functions to build them for my theme purposes. ^.^;
1 Like
afulki
June 15, 2017, 4:56pm
7
Here you are:
<div class="row">
<form accept-charset="UTF-8" action="/types/15" class="col s12" method="post"><input name="_method" type="hidden" value="put"><input name="_csrf_token" type="hidden" value="Bj8QJzETEUAKRCg8fk8bH2ERGzUCNgAA5ReTrCIpAsII18yMVzvPMA=="><input name="_utf8" type="hidden" value="✓">
<div class="row">
<div class="input-field col s12">
<label class="control-label" for="type_name">Name</label> <input class="form-control" id="type_name" name="type[name]" type="text" value="TestType">
</div>
</div>
<div class="row">
<div class="input-field col s12">
<label class="control-label" for="type_description">Description</label> <input class="form-control" id="type_description" name="type[description]" type="text" value="This is a new test type with enumeration">
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input type="checkbox" class="form-control filled-in" id="type[Account-box]" value="true" />
<label for="type[Account-box]">Account</label>
<input type="checkbox" class="form-control filled-in" id="type[Advisor-box]" value="true" />
<label for="type[Advisor-box]">Advisor</label>
<input type="checkbox" class="form-control filled-in" id="type[Exception-box]" value="true" />
<label for="type[Exception-box]">Exception</label>
<input type="checkbox" class="form-control filled-in" id="type[Holdings-box]" value="true" />
</div>
</div>
Thanks for the help
1 Like
afulki
June 15, 2017, 4:57pm
8
Hmm, I thought I had the name, but let me go try that,
Thank you!
1 Like
Nope no name
s:
<input type="checkbox" class="form-control filled-in" id="type[Account-box]" value="true" />
afulki
June 15, 2017, 5:05pm
10
Thank you both,
I now have data coming back from the checkbox!
1 Like