Suman Das
Suman Das

Reputation: 311

How to add required true on collection_select type field

I have already tried to add required=> true and :prompt=>"select..." on the collection_select field, but every time I get syntax error. How to solve this error which is below.

.field
  = f.label "Receiver"
  = f.collection_select(:receiver_admin_id, 
        Admin.agent_and_admin(current_admin.id, current_admin.parent_master_agent_id), 
        :id, 
        :agent_name, 
        :prompt => 'Select receiver', 
        {multiple: true}), 
      :required => true
  = f.collection_select(:receiver_admin_id, 
        Admin.agent_and_admin(current_admin.id, current_admin.parent_master_agent_id), 
        :id, 
        :agent_name, 
        :prompt => 'Select receiver', 
        {multiple: true, required: true})

  = f.collection_select(:receiver_admin_id, 
        Admin.agent_and_admin(current_admin.id, current_admin.parent_master_agent_id), 
        :id, 
        :agent_name, 
        :prompt => 'Select receiver',
        {}, 
        {multiple: true}), 
      :required => true
  = f.collection_select :receiver_admin_id, 
        Admin.agent_and_admin(current_admin.id, current_admin.parent_master_agent_id), 
        :id, 
        :agent_name, 
        :prompt => 'Select receiver', 
        {multiple: true}, 
        :required => true

Upvotes: 0

Views: 361

Answers (2)

sensadrome
sensadrome

Reputation: 482

Your were close almost everytime :) From the docs in the rails guide you have:

collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})

Using the form helpers (f.) mean that the object (first argument) is taken care of so you're left with:

method, collection, value_method, text_method, options, html_options

Both the multiple and required need to be in the html_options so:

= f.collection_select(:receiver_admin_id, Admin.agent_and_admin(current_admin.id, current_admin.parent_master_agent_id), :id, :agent_name, { :prompt => 'Select receiver' }, multiple: true, :required => true)

Upvotes: 0

mehedi
mehedi

Reputation: 466

Try this please:

  = f.collection_select :receiver_admin_id, Admin.agent_and_admin(current_admin.id, current_admin.parent_master_agent_id), :id, :agent_name, {prompt: 'Select receiver'}, {multiple: true, required: true}

Upvotes: 0

Related Questions