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

Already on GitHub? Sign in to your account

Disabled checkbox hidden field not disabled #498

Closed
simmerz opened this Issue Feb 1, 2011 · 20 comments

Comments

Projects
None yet
5 participants

simmerz commented Feb 1, 2011

The hidden field created alongside a checkbox isn't disabled when the main one is disabled using :disabled => true. This means that rather than no value being submitted for that field, the "negative" value is submitted instead.

Collaborator

sobrinho commented Feb 1, 2011

Can you provide a patch or a test case?

Collaborator

sobrinho commented Feb 1, 2011

Can you submit as pull request? I will apply the spec and fix the issue.

simmerz commented Feb 1, 2011

Submitted. Thanks :)

Owner

justinfrench commented Feb 8, 2011

Looks like this was resolved in 91d5292, closing.

simmerz commented Feb 9, 2011

Doesn't appear to be fixed. The test still fails on 1.2-stable and master.

Owner

justinfrench commented Feb 9, 2011

Right. Re-opening.

Owner

justinfrench commented Feb 9, 2011

Ok, this is tricky. I made the test pass locally, but the test is invalid. There's one hidden input for one or more checkboxes. Disabling the hidden input as per the test (which has two checkbox choices, one of which is disabled) would cancel out the whole purpose of the hidden input (when trying to uncheck all choices).

The only case where this test is correct is when all checkbox choices are also disabled. I can probably write code for that, but I'm not convinced that it's a likely use case.

Maybe I'm missing something?

Leaving the test broken for now, have pushed to branch GH-498.

simmerz commented Feb 13, 2011

My use case is this:

An admin wants to manage users, including himself. However, the admin shouldn't be able to remove himself from the admin list, and that is managed by a single checkbox. Disabling this lets him visually see that he is an admin, but then a submit actually removes him from the list because of the hidden field.

Owner

justinfrench commented Feb 14, 2011

For cases like that with just one choice, I would use a :boolean input, not a :checkboxes input. This may not map perfectly to your model, but it makes sense in terms of form and params structure.

<%= f.input :admin, :as => :boolean %>

Under the hood, you may need to create some accessors on the model to accept this attribute, and a before_save hook in the model to translate this simple accessor into what your model expects (complex associations maybe?).

Let me know how you go, will close this in a week or so if I haven't heard back.

simmerz commented Feb 14, 2011

It already is a boolean field, and maps correctly. The issue still persists though, and the usecase is as described above. The db field is a boolean too.

Owner

justinfrench commented Feb 14, 2011

Ok, my response was based on the tests that were added to the checkboxes input specs. Now that we have that cleared up I can shift the test over to the boolean input and make it pass pretty easily. Thanks.

simmerz commented Feb 14, 2011

Damn! I put it in the wrong place? Sorry! It did cross my mind after your last comment though.

Owner

justinfrench commented Apr 4, 2011

Fixed in 7c4c4f3 on refactor branch, will be in 2.0. Looking now at how hard it will be to backport into 1.2-stable.

Owner

justinfrench commented Apr 4, 2011

Awesome, was easy to add into 1.2-stable (17cc8c2), will be in next 1.2.x release. Closing.

sterrym pushed a commit to sterrym/formtastic that referenced this issue Apr 26, 2011

Revert "Add spec to test for disabled hidden field with disabled chec…
…kbox" until we have the issue properly understood (see GH-498 and branch of same name for WIP).

This reverts commit 91d5292.

sterrym pushed a commit to sterrym/formtastic that referenced this issue Apr 26, 2011

robertwahler pushed a commit to robertwahler/formtastic that referenced this issue Jul 13, 2011

Revert "Add spec to test for disabled hidden field with disabled chec…
…kbox" until we have the issue properly understood (see GH-498 and branch of same name for WIP).

This reverts commit 91d5292.

Hmmm, I'm using ActiveAdmin with Formtastic 2.1.1 and check_boxes don't seem to be getting my disabled setting.

example:

# this is not disabled
f.input :shares, input_html:{disabled:true}, as: :check_boxes, collection:f.object.shares 

# this is disabled
f.input :shares, input_html:{disabled:true}, as: :select, multiple:true, collection:f.object.shares 

#this is disabled
f.input :notes, input_html:{disabled:true}, as: :boolean 

Am I missing something here or is there a bug in Formtastic?

Owner

justinfrench commented Mar 22, 2012

@gabeodess I believe this issue was around :boolean, not :check_boxes, but the other answer is "you're missing the documentation" :) The docs for CheckBoxesInput (whilst it could be better) shows that :disabled is a top-level option, rather than an :input_html option. It's a little confusing, but I think this was because you can pass in an array of options to be disabled. http://rdoc.info/github/justinfrench/formtastic/Formtastic/Inputs/CheckBoxesInput

:disabled can be used to disable any checkboxes with a value found in the given Array
<%= f.input :categories, :as => :check_boxes, :collection => ["a", "b"], :disabled => ["a"] %>

Excellent! Took a little finagling, but finally got it to work:

f.input :shares, disabled:f.object.shares.map(&:id), as: :check_boxes, collection:f.object.shares

Thanks @gabeodess and @justinfrench ! This is a five year old thread but it helped. I checked the documentation first but it led me to use an array of objects rather than an array of ids of the objects. It would be easier to understand if we clarify the document explaining to use an array of ids than objects themselves.

Owner

justinfrench commented Feb 3, 2017

@maxpark-jd documentation patch welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment