0

I have a form to submit recipes, but I need to check if the user selected some ingredents and implements

<div class="field">
    <label> ingredientes </label>
    <br>
    <% @ingredients.each do |ingredient| %>
    <div class="field">
      <dd><%= check_box_tag "Ingredients[]", ingredient.id %> <%= ingredient.nombre %></dd>
    </div>
    <% end %>
  </div>

  <div class="field">
    <label> implementos </label>
    <br>
    <% @implements.each do |implement| %>
    <div class="field">
      <dd><%= check_box_tag "Implements[]", implement.id%> <%= implement.nombre %></dd>
    </div>
    <% end %>
  </div>

Anyway, I want to limit the number of ingredents to 10 and implements to 5, but i think I can do that in the controller but my problem is I dont know how to validate that the user selected at least one checkbox, if I use

:required => true

it make all checkboxes checked by default and I dont want that

Sage Harpuia
  • 348
  • 2
  • 13
  • Is there any reason you don't want to do this in the same place/way that you confirm there are no more than 10 ingredients / 5 implements? For instance, you could just grab the array in the controller and an `if ingredients.length > 10 do_something elsif ingredients.length < 1 do_something_else`. – ConnorCMcKee Jan 18 '16 at 18:32
  • Not really, I just throught maybe there was someway to do it in a easiest way – Sage Harpuia Jan 18 '16 at 18:33
  • 1
    Generally you can take one of two approaches to form validation: pre- and post- submission. It is generally considered unwise to mix the two, so if you're already returning messages from the controller, you should keep it that way. I don't know of a rails form shorthand for this. If you were trying to implement pre-submission validation for these fields, your best bet would be javascript disabling the submit button unless at least one item is selected. – ConnorCMcKee Jan 18 '16 at 18:36

0 Answers0