Simple Form: checkbox's value is always "1" - ruby-on-rails

I have a form and need to add a GDPR checkbox.
If the checkbox is clicked (= checked), the checkbox's value should be true (or 1). If not, then the value should be false (or likely nil).
This is how I render the checkbox:
= f.input :gdpr, as: :boolean, required: true, label: false, checked_value: true, unchecked_value: false, input_html: { }
However, when I look at the generated HTML, I see that (probably) by default the checkbox's value is 1 - how is that possible?
EDIT: This is the rendered HTML:
<input name="obj[gdpr]" type="hidden" value="open">
<input required="required" aria-required="true" type="checkbox" value="true" name="obj[gdpr]" id="obj_gdpr">
How do replaced that 1 with nil?
Thank you in advance.

value attribute is not the default value of the checkbox. This attribute is submitted as input's value to the server if checkbox is checked.
E.g.
<label>Completed?</label>
<input type="checkbox" value="completed" name="status"
This will be submitted as status=completed.
To check if checkbox is checked use checked attribute of DOM node in JS.
Also see MDN explanation: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#value

Related

rails check_box_tag default unchecked value

Is there any way to set check_box_tag unchecked value to any other value than nil? Something like:
check_box_tag :active, ['true', 'false'], ...
In the HTML specifications unchecked checkboxes are not included when sending the form. So there is no "unchecked value" for a checkbox.
There is a workaround though - you include a hidden input with the same name attribute as the checkbox:
<input type="hidden" name="active" value="deader then dead">
<input type="checkbox" name="active">
Since key/value pairs have to be sent in the same order they appear in the form the checkbox will override the hidden input if it is checked.
The higher level Rails checkbox helper has this workaround built in.

Empty value in activeadmin for checkboxes list

I use checkboxes list for enum to select multiple values in activeadmin:
c.input :weekdays, as: :check_boxes, collection: BusinessHour.weekdays.keys
For some reason there is hidden value added:
<input type="hidden" name="listing[listing_prices_attributes][0][weekdays][]" id="listing[listing_prices_attributes][0]_weekdays_none" value="" autocomplete="off">
So when form is submitted it actually adds also empty value "" in array and makes problem.
Tried also add include_hidden: false, include_blank: false but it also doesn't change empty string value.
Any reason why it works this way?

How to remove area-require attribute in rails form?

This is a checkbox which has required attribute true
This form belongs to form component made using simple form
= form.input :terms, label: "hello", as: :boolean, required: true
But when this is converted into HTML (rendered by RAILS), we get this result
<input class="boolean required" required="required" aria-required="true" type="checkbox" value="1" name="sample_name" id="sample_id">
The above code contains an extra attribute called aria-required which comes with require attribute from HAML file.
It is said that simple form automatically adds the this aria-required attribute.
How to remove the aria-required attribute from the form without removing required attribute?

Rails Simple Form single checkbox twice in querystring

I have a search form which I return to after searching, to be able to refine the search. I just added a checkbox to that form.
In the view:
<%= f.input :available_tomorrow, as: :boolean, label: false,
inline_label: t('public.search.form.available_tomorrow.label'),
input_html: { name: :available_tomorrow, value: params[:available_tomorrow],
id: :available_tomorrow } %>
In the model:
attr_reader :available_tomorrow
HTLM produced:
<div class="form-group boolean optional search_available_tomorrow">
<input name="available_tomorrow" type="hidden" value="0">
<label class="checkbox">
<input class="boolean optional" id="available_tomorrow" name="available_tomorrow" type="checkbox" value="1">
Available tomorrow
</label>
</div>
When I check the box, all search parameters appear fine in the url querystring but that one:
&available_tomorrow=0&available_tomorrow=1
Looks like the value property of both field is sent, and neither changes. If I uncheck the box, I only get &available_tomorrow=0 in the querystring. The second part is only added if the checkbox is checked.
Everything works as intended (the search does return the right results depending on the checkbox state, the checkbox is in the right state when the search form is updated). But that querystring is ugly with both available_tomorrow parameters, looks like the first one should never appear. Ideas?
This is normal behavior for a form with a checkbox when you POST the form: If the checkbox is not checked, only the hidden field with value 0 is sent with the formdata. If the checkbox is checked, the input with the value 1 is also sent.
The hidden field for the checkbox is always sent, this is because otherwise no value for the checkbox would be sent.
Long story short: don't mind the querystring in your search url, nobody will ever look at them. :)

Is This Unexpected Behavior for the CheckBox HtmlHelper?

My view page contains a search-by-example form with the following checkbox code:
<td>
<label for="HasProcessErrors">Has Errors:</label>
<%= Html.CheckBox("HasProcessErrors", crit.HasProcessErrors) %>
</td>
The crit object HasProcessErrors property is a boolean whose initial value is false. When I view the source of my rendered page, I see that the helper has generated the following HTML:
<td>
<label for="HasProcessErrors">Has Errors:</label>
<input id="HasProcessErrors" name="HasProcessErrors" type="checkbox" value="true" /><input name="HasProcessErrors" type="hidden" value="false" />
</td>
Have I used the CheckBox helper incorrectly here, or is something odd going on? It seems like it should generate an input of type checkbox with checked = "".
Thanks for any ideas.
Yes this is correct.
The semantics of a checkbox are a little bit different from what you may think; instead of posting a value indicating its checked/unchecked state, a checked checkbox posts whatever is in its ‘value’ attribute, and an unchecked checkbox posts nothing.
As there is also a hidden field with the same name, if you debug your form submit, you will find a checked checkbox has the value 'true,false' whilst an unchecked box has the value 'false'
You can determine if a checkbox is checked by testing if it contains 'true'.
public ActionResult(FormCollection form)
{
bool checked = form["checkbox_id"].ToString().Contains('true');
}

Resources