DavyJonesLocker / client_side_validations

Client Side Validations made easy for Ruby on Rails
MIT License
2.69k stars 404 forks source link

Error messages gets displayed with each checkbox when working with collection_check_boxes #898

Open as-rikoouu opened 1 year ago

as-rikoouu commented 1 year ago

Steps to reproduce*

Code snippet added below

Expected behavior*

It would be ideal for the message to appear only once, maybe with the label and not with every checkbox.

Actual behavior*

When submitting the form without any selection, the message should appear once for each checkbox (please excuse the CSS)

Screenshot 2022-11-14 at 11 24 51 PM

System configuration*

Rails version: 6.1.4

Ruby version: 2.7.4

Client Side Validations version: latest

Code snippet from your model of the validations*

In the model, I have a custom method that checks and returns an error if no checkbox was selected. Basically, we don't want to show a default selected checkbox and want at least one to be selected by the user before the form can be submitted

serialize :shipping_carrier_type_ids, Array

validate :shipping_carrier_type_presence

  def shipping_carrier_type_presence
    errors.add(:shipping_carrier_type_ids, "Select at least one Shipping Carrier Type") if shipping_carrier_type_ids.reject(&:empty?).blank?
  end

The whole form code from your template*

<%= f.collection_check_boxes :shipping_carrier_type_ids, ShippingCarrierType.all, :id, :name, {}, {class: 'form-control bb-field'} %>

The resulting HTML*

Screenshot 2022-11-14 at 11 24 51 PM

Browser's development console output*

Additional JavaScript Libraries*

If your issue depends on other JavaScript libraries, please list them here. E.g: Bootstrap Modal v3.3.7, jQuery UI Datepicker 1.12.4.

Repository demostrating the issue

Debugging CSV issues is a time consuming task. If you want to speed up things, please provide a link to a repository showing the issue.


* Failure to include this requirement may result in the issue being closed.