How to remove area-require attribute in rails form? - ruby-on-rails

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?

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?

Simple Form: checkbox's value is always "1"

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

Ruby on Rails checkbox not saving data

I have a checkbox
<%= f.check_box :anonymous %>
And my table has a column anonymous which is true or false.
Code generated in html:
<input name="comment[anonymous]" type="hidden" value="0" />
<input id="comment_anonymous" name="comment[anonymous]" type="checkbox" value="1" />
Now, for some reason when I add data it's not saving if my anonymous checkbox is checked or not.. it's not changing data in database.. All other fields gets saved except anonymous.
What can be the problem ?
Use #check_box_tag instead:
<%= check_box_tag(:anonymous) %>
From the official guides:
Array parameters do not play well with the check_box helper. According
to the HTML specification unchecked checkboxes submit no value.
However it is often convenient for a checkbox to always submit a
value. The check_box helper fakes this by creating an auxiliary hidden
input with the same name. If the checkbox is unchecked only the hidden
input is submitted and if it is checked then both are submitted but
the value submitted by the checkbox takes precedence. When working
with array parameters this duplicate submission will confuse Rails
since duplicate input names are how it decides when to start a new
array element. It is preferable to either use check_box_tag or to use
hashes instead of arrays.

Rails and simple form: novalidate attribute doesn't work

my login page( build with simple form) add by default html attributes for browser validation on email input that chrome doesn't recognize and show "Please match the requested format."
Maybe is Chrome bug(on firefox works), so have tried to disable browser validation with simple form config
SimpleForm.html5 and SimpleForm.browser_validations(false by default), restarted rails but remain the same input:
<input autofocus="autofocus" class="string email optional form-control
input-xlarge" id="customer_email" maxlength="255"
name="customer[email]" pattern="\A[^#\s]+#([^#\s]+\.)+[^#\s]+\z"
size="255" type="email">
have tried also to add on form html: {novalidate: true}, same output
finally have tried to add on input_filed :novalidate => true, the html output change to:
<input autofocus="autofocus" class="string email optional form-control
input-xlarge" id="customer_email" maxlength="255"
name="customer[email]" pattern="\A[^#\s]+#([^#\s]+\.)+[^#\s]+\z"
size="255" type="email" novalidate="novalidate">
but browser validation and chrome error is present.
Any idea to resolve?
PS: Use Bootstrap and the login form is from Devise resource.
You can remove the pattern attribute from the input element that is causing a problem. You just need to set pattern: false on the input field.
So your input field might look something like this:
<%= f.input_field :email, type: 'email', required: true, autofocus: true, class: 'form-control', pattern: false %>
(nil doesn't work; it has to be false.)
This worked for me in Rails 4.

Resources