Radio buttons #24

Closed
bcardarella opened this Issue Mar 30, 2011 · 18 comments

Projects

None yet

7 participants

@bcardarella
Contributor

No description provided.

@bcardarella
Contributor

They basically don't work. Pretty stupid of me to have this as an oversight.

@bcardarella
Contributor

Somewhat complete, first steps: 4eaa089

@bcardarella
Contributor

Radio buttons are a bitch.

Semantically there is no way to work with a group of radio buttons in Rails by default. So each will have its own label and error message. A possible solution would be to create a new form helper that worked like:

f.radio_buttons :category, [:one, :two, :three]

alternative

f.radio_buttons :category, [[:one, 'One',], [:two, 'Two',], [:three, 'Three']]

This would create all three radio buttons, assigning the value from the array, if a 2-dimensional array is passed you can set the label explicitly. Otherwise the label is derived from the value name. (titleized?)

That would work, the only problem is this begins to go outside the scope of what ClientSideValidaitons is supposed to be.

@bcardarella bcardarella was assigned Apr 10, 2011
@gmile
Contributor
gmile commented Apr 15, 2011

Should you address the issue to rails team? Or even pull a request?

@bcardarella
Contributor

@gmile The problem is the way ActionView's FormBuilder generates inputs. Each one is wrapped when there is an error on the associated model's field. So each radio button gets wrapped and each wrap, if you uncomment the code from the initializer, generates an error message. Grouping them together would be difficult with the way FormBuilder currently works. In addition, it would imply labels being injected which violates the way FormBuilder works all together.

I'm not certain including my own helper is the solution here. That starts to get outside the scope of what ClientSideValidations is supposed to be. Maybe I could add an additional gem just for this? I'm not certain.

@bcardarella
Contributor

I've decided to never support this. It is opens up too many issues with how to solve it. If you really want radio buttons and ClientSideValidations you should use another form builder. I suggest SimpleForm.

@bcardarella bcardarella closed this May 9, 2011
@sxross
sxross commented Jun 18, 2011

Note: Recognizing that people will use radio buttons, it's possible to at least ignore them even if they are validated on the server side by doing this in rails.validations.js:

$.fn.isValid = function(validators) {
  if ($(this[0]).is('form')) {
    return validateForm($(this[0]), validators);
  } else {
    if(this[0]['type'] == 'radio')   // <== This ignores radios
      return true;
    else
      return validateElement($(this[0]), validators[this[0].name]);
  }
}
@bcardarella
Contributor

I agree. I'll add it.

@bcardarella bcardarella reopened this Jun 18, 2011
@bcardarella
Contributor

This is available on master. It will be part of the 3.1 branch. (I've backported it to 3-0-stable as well)

@brandondrew

A nice summary of the situation would be useful to have in the README, since reading disparate comments and trying to string the logic together is not always as easy for outside parties as it appears to be to those who were writing them in the first place (and who are therefore much closer to the situation).

If I understand correctly, the situation is as follows:
You're not supporting client-side validations with Rails' default form builder, but you are with SimpleForm and Formtastic. So if you want to validate radio buttons on the client, you need one of those form builders, otherwise your radio buttons will simply be ignored client-side, and handled on the server.

Is that accurate?

@fringd
fringd commented Sep 22, 2011

i don't get why this is hard, maybe somebody can clarify for me, where exactly are thiings breaking down? as i see it there are a fews steps

  1. get the value of the radio (taking into account the other inputs with the same name)
  2. validate as usual
  3. display an error somewhere near the radio.

i /think/ that the problem is in step 3? if that's right, would it at least be possible to support this somewhat? displaying errors near the first, the last, all? also... i'm using simple_form which generates fairly standard markup that does group the radio buttons.

can somebody clarify this?

@bcardarella
Contributor

@fringd The issue isn't with the validation but with the message placement. With both SimpleForm and Formtastic this is trivial as both of those FormBuilders group the radio inputs together into one object. But with Rails' FormBuilder each radio button option is a separate object, yet they are logically treated as a group so the validation message has no context for placement.

@bcardarella
Contributor

@fringd I'm ClientSideValidations 3.2 is going to go down a different route. Out of the box it will only support ActionView::FormBuilder then you can add in FormBuilder adapters for ClientSideValidations. In your case you would add the soon to exist client_side_validations-simple_form (I haven't decided which name should go first) gem. This would then allow me to add support for radio buttons.

Unfortunately, I've been pretty slammed with work. (as evidenced by the piling issues) I cannot estimate when this will be done.

@fringd
fringd commented Sep 22, 2011

@bcardarella, i like csv::sf for what it's worth... i guess i'll just try to modify it to support simple_form radios myself... if i come up with anything that might be shareable quality i will post a diff :)

@eirc eirc added a commit to e-travel/client_side_validations that referenced this issue Jan 9, 2012
@eirc eirc Fix issue #24 with fix by @sxross. af38b25
@lacco
lacco commented Jun 27, 2012

Does anybody know the current state of radio button support for formtastic? As far as I see https://github.com/dockyard/client_side_validations-formtastic isn't very active, has somebody else put some work into it?

@bcardarella
Contributor

I'm willing to accept people joining as repo collaborators if one wants to take ownership of any if the CSV plugins


Brian Cardarella
Principal at DockYard
Visit us: http://dockyard.com
Call us: (855) DOCK-YRD
Follow me on Twitter: http://twitter.com/bcardarella
Follow us on Twitter: http://DockYard

On Jun 27, 2012, at 8:40 AM, Kai Schlichtingreply@reply.github.com wrote:

Does anybody know the current state of radio button support for formtastic? As far as I see https://github.com/dockyard/client_side_validations-formtastic isn't very active, has somebody else put some work into it?


Reply to this email directly or view it on GitHub:
#24 (comment)

@lacco
lacco commented Jul 2, 2012

I tried to extend client_side_side_validations-formtastic to support radio buttons - and ended up copying much of the client_side_validations code. I didn't find a proper way to hook into the code, as far as I see a little more object orientation is needed to make it work. Nevertheless, you find the working implementation of radio validations here: https://gist.github.com/72a540db6994717ae281

@kylesziv

I'm currently implementing this in a project but I'm not getting it to work. Was this fixed?

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