Rails 3 validation: two available checkboxes, can't check them both - ruby-on-rails

I have a very simple object that needed an approved checkbox and a Declined checkbox
How can I check to make sure that both aren't checked?
A user should only check one, not both.

As #Christopher Marshall pointed out - this functionality is accomplished by the radio button type. Just change type="checkbox" to type="radio" and make sure the name is the same

Related

Conditional Validation ie. Requiring a field only if a certain radio button is checked?

This is more of a pointing in the right direction sort of thing. I'm currently working on a project where a handful of fields will be hidden until a radio button is checked, therefore also not required until then. So tick the specific radio button, fields show up and they are now required on submit with the [Required] DataAnnotations attribute in the model.
I went down the path of trying to use MVC Foolproof and [RequiredIf], but didn't have much luck considering the outdated js files necessary and was wondering if someone else had a simpler solution.
I appreciate any input. I feel like this isn't too uncommon of a task but had a lot of difficulty finding a solution via Google.
I am sure you can accomplish this with using Javascript/Jquery.
Like so:
if($('#idNameOfRadioBtn').is(':checked')){
$('#idOfFieldNameThatIsNowRequired').attr('required');
}
else{
$('#idOfFieldNameThatIsNowRequired').removeAttr('required');
}
Let me know if this helps!
I suggest that you use angularjs for this as it is built for it. If you are not familiar with angular validation, here is a great article in scotch where it gives a really good demonstration. Good luck!
Hide and show fields based on ng-if directive and make field required using the required attribute. That's it!
<input type="text"
name="name"
class="form-control"
ng-model="user.name"
ng-if="user.required"
required>
Angular Validation

checkbox is checked activeAdmin

I got a problem...
I had to use active admin for create a custom page -_-.. So I can't interact with my controller from my view.
So I've create many checkbox like this in my view,
input type="checkbox" class="filter" autocomplete="off" name="lundi" value="0" checked>Lundi
I'd like to use them in the same view I want to know if the checkbox is checked or not. So if someone ad a tricks? I'm a new user of rails.
You can use jQuery to check if checkbox is checked or not and do some thing on that decision, send ajax call or something else.
if ($("[name='lundi']").is(":checked")){
console.log("Do Something.");
}
Let me know if you need further help.

How to implement a toggle button to set a Rails boolean field

I have a Rails form with a checkbox on a boolean field.
I want to replace the checkbox with a toggle button, using Twitter Bootstrap.
Is there anything baked into Rails to provide this kind of functionality? Or will I need to write a script to update the checkbox value when the toggle button is clicked?
Thanks for any pointers. After a lot of searching, I've been unable to find anything that describes this.
At this point in time, the toggle Bootstrap buttons do not have the correct markup to work out of the box with forms (source).
You can wrap around the controls to insert the correct values into your parameters map using hidden fields. If you're using AJAX requests, you can simply include them in your parameters there.
In the case of the single toggle, you could implement a function as simple as this to set the hidden value.
In your form:
<input name="toggled" value="false" />
Evaluated before submission:
function isToggled() {
$('input[name="toggled"]').value($('#toggleButton').hasClass('active'));
}

Double CheckBox Creation mistake?

I have a foreach wich populates a table and one field of the line tables is:
<% foreach (var item in Model.List){%>
<td align="center">
<%: Html.CheckBox(item.ID.ToString(),item.isChecked)%>
</td>
<%}%>
Inside my post function I was trying to get the Request.Form["45"]( 45 is a sample ID) and saw that the value "true,false" was being recieved.
Taking a look into the Code generated, I just saw that:
<input name="45" type="checkbox" value="true" /><input name="45" type="hidden" value="false" />
How its possible since Im just asking to generate one input? I dont know too if Html.CheckBoxForis better to use in this case
Thanks !
You can read the explanation here
the same approach that both Ruby on Rails and MonoRail use.
When you submit a form with a checkbox, the value is only posted if
the checkbox is checked. So, if you leave the checkbox unchecked then
nothing will be sent to the server when in many situations you would
want false to be sent instead. As the hidden input has the same name
as the checkbox, then if the checkbox is unchecked you'll still get a
'false' sent to the server. When the checkbox is checked, the
ModelBinder will automatically take care of extracting the 'true' from
the 'true,false'
Why don't you use model so that model take care of it. Using request object is not recommended in asp.net-mvc You may also use formcollection parameter in your actionresult and handle this scenario using this approach.
You may always use html tags if helpers don't fit in your requirement.

How to check a checkbox in capybara?

I'm using Rspec and Capybara.
How can I write a step to check a checkbox? I've tried check by value but it can't find my checkbox. I'm not sure what to do, as I have in fact same ID with different values
Here is the code:
<input id="cityID" type="checkbox" style="text-align: center; opacity: 0;" value="61" name="cityID">
<input id="cityID" type="checkbox" style="text-align: center; opacity: 0;" value="62" name="cityID">
<input id="cityID" type="checkbox" style="text-align: center; opacity: 0;" value="63" name="cityID">
I found the following worked for me:
# Check
find(:css, "#cityID[value='62']").set(true)
# Uncheck
find(:css, "#cityID[value='62']").set(false)
It's better not to create multiple elements with the same id, so that (and not only for that) you can easily check/uncheck a checkbox with elegant
check 'cityID'
uncheck 'cityID'
If one can not avoid multiple elements with the same id and still needs to check a checkbox with certain value, he can do so with
find(:css, "#cityID[value='62']").set(true)
find(:css, "#cityID[value='62']").set(false)
More information on capybara input manipulations can be found here
When running capybara test, you got the page object. This you can use to check/uncheck any checkboxes. As #buruzaemon already mentioned:
to find and check a checkbox by name, id, or label text.
So lets assume you got a checkbox in your html like:
<label>
<input type="checkbox" value="myvalue" name="myname" id="myid">
MyLabel
</label>
You could check this with:
page.check('myid')
page.check('MyLabel')
page.check('myname')
Uncheck is the same just use page.uncheck method.
I think you may have to give unique ids to your form elements, first of all.
But with regards to Capybara and checkboxes, the Capybara::Node::Actions#check instance method will allow you to find and check a checkbox by name, id, or label text.
If the box is associated with text, e.g. 'Option 3', then as of capybara 3.0.3 you can just do
check 'Option 3'
I know this is an older question, but I have been working through this myself, and having tried all of the above, this is what finally worked for me:
find("input[type='checkbox'][value='#{cityID.id}']").set(true)
Hope this is helpful to someone. I am using Capybara 2.4.4.
An old topic but another solution is:
check('Option 3', allow_label_click: true)
Had some issues with custom checkbox which is hidden behind label element. Needed a allow_label_click: true.
With reference to this blog post,
check 'checkbox[name]', allow_label_click: true
For cases where there is a link in your label like "I agree to terms and conditions", the above code will open the page, which is not what you want.
Do this instead.
find(:css, "#checkbox_id", visible: false).execute_script('this.checked = true')
you can also use :xpath instead of :css if you have some problems finding it.
find(:xpath , '//*[#id="example"]').set(true)
on Chrome (and surely other browsers), you can "inspect element" and then by right clicking on the element you are interested in, there is 'copy xpath' if you don't know what xpath was, now you do.
You can also check that all the checkboxes are not checked with this example.
all('input[type=checkbox]').each do |checkbox|
checkbox.should_not be_checked
end
.set(true) didn't work for me so I had to call .click:
find(...).click
I believe this looks more elegant:
find_by_id("cityID").set(true)
Tested on Rails 7. Works fine.
To select the checkbox
check 'name_of_checkbox'
check find(".whenever input")[:id]
I think this will make capybara wait for any event listener attached to that input, which sometimes is a pain-in-the-ass if it doesn't waits ....
If that input doesn't have an ID, choose another property (there must be one)...

Resources