1

How do I check from the client-side if there is at least one checkbox being ticked here?

= form_with model: @form, url: generate_matches_tournament_path(@tournament), method: :post, local: true do |f|
  .field.justify-center.mb-4.mt-3.w-full.form-group
    = f.label :available_weekdays, 'Available Weekdays', class: "block text-sm font-bold mb-2"
    .checkbox_container.rounded-xl.p-4
      - weekdays = [['Monday', 'Mon'], ['Tuesday', 'Tue'], ['Wednesday', 'Wed'], ['Thursday', 'Thu'], ['Friday', 'Fri'], ['Saturday', 'Sat'], ['Sunday', 'Sun']]
      = f.collection_check_boxes :available_weekdays, weekdays, :second, :first do |b|
        .checkbox_item
          = b.check_box(class: 'checkbox')
          = b.label(class: 'ml-2') { b.text }

    - if @form.errors[:available_weekdays].any?
      .text-red-500.text-xs.mt-1
        = @form.errors[:available_weekdays].first

I am using Rails 7, Vite.

For context, I already have a server-side check and display the errors, but that is after the request is sent, which I kind of want to avoid in the first place.

I tried simply add required: true in the

= f.collection_check_boxes :available_weekdays, weekdays, :second, :first, required: true do |b|

but to no one's surprise, this did not work.

I am aware that I can use javascript to make this work, but I'd prefer a built-in or a more Rails way to do this, so that I can make use of this: validation tooltip

1
  • Could you try to add required: true for each checkbox_item instead of whole collection_check_boxes => b.check_box(class: 'checkbox', required: true) # Add 'required: true' Commented Nov 14, 2024 at 8:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.