trailblazer / formular

Form builder for Ruby. Fast, Furious, and Framework-Agnostic.
MIT License
81 stars 17 forks source link

Support for multiple checked checkboxes #57

Open bigforcegun opened 6 years ago

bigforcegun commented 6 years ago

Checkboxes with collection now support only one checked checkbox, when select with multiple: 'multiple' support multiple selected items.

I suggest change method in Formular::Element::Modules::Checkable

def is_checked?
    !options[:checked].nil? || reader_value == options[:value]
end

to something like this

def is_checked?
    return !options[:checked].nil? || reader_value == options[:value] unless reader_value.is_a?(Array)
    reader_value.map(&:to_s).include?(options[:value].to_s) # TODO Perf improvement here - do we need the map?
end

aka in Select Element

def item_is_selected(option_val, current_val, multiple)
    return option_val == current_val.to_s unless multiple && current_val.is_a?(Array)
    current_val.map(&:to_s).include?(option_val) # TODO Perf improvement here - do we need the map?
end

Why?

It's unusual behavior in form builder with checkboxes. Array mean multiple choises and multiple checked elements.

bencallaway commented 6 years ago

I agree with @bigforcegun. A set of checkboxes permits zero or more checked values. I would expect given a :collection of possible values I can designate which of those are rendered with a checked attribute. I'm working on a PR, but this is a little tricky given that the :default and :with_collection contexts both use the :value option (differently), and the fact that its default value is set the same way in both of these contexts (via the default_checked_value method.

Does a collection of checkboxes need a default value? This may only be useful for single boolean checkboxes.