0
<%= form_for @organization do |f| %>
  <%= render partial: 'shared/error_messages', locals: { object: f.object } %>

  <label id="icon" for="country"></label>
  <%= f.select :country, [['Afghanistan', 'AF'],
                          ['Albania', 'AL'],
                          # etc.,
                          ['Zimbabwe', 'ZW']
                         ], {:prompt => 'Please select location', required: true} %>

  <label id="icon" for="id_number"></label>
  <%= f.text_field :id_number, placeholder: 'e.g.: 123abc' %>
  <%= f.check_box(:no_id) + "&nbsp <i>No ID</i>".html_safe %>
<% end %>

In a signup form I have the above code. How can I achieve the following for this form?

1) How to make country required? It currently does accept it when no country is selected despite required: true. Perhaps it also sees the "Please select location" as a value?

2) How to make it required to either enter an id_number or to check the box for no_id, and it's not possible to have both. I would like this requirement for the form as well as at a model level.

Update: I added the model validation below for the 2nd requirement, which seems to work:

validate  :id_or_no_id

def id_or_no_id
  if (self.id_number.nil? || self.id_number.empty?) && self.no_id != true
    errors.add(:id_number, "Need id or need to select checkbox")
  elsif (self.id_number && !self.id_number.empty?) && (self.no_id && self.no_id == true)
    errors.add(:id_number, "Can't have both id and checked checkbox")
  end
end

How can I get the form validation for country to work? If I change the last line for f.select to ], {}, {:required => true, :placeholder => 'Select your location...'} %> an empty line is at the top of the drop-down box (so the placeholder isn't working) and if not selecting a country, then the form validation works in that it asks to select a country. But now the placeholder isn't working...

Marty
  • 2,132
  • 4
  • 21
  • 47
  • 1
    I assume you're using Rails. Are the fields in the form tied to a model? Can you show the entire form_for block? The answer is probably that you would use model validations (see http://guides.rubyonrails.org/active_record_validations.html) but would need to know more to be sure. – rdnewman Sep 18 '15 at 22:53
  • Rereading your questions implies that you might need some javascript too. But not necessarily if the controller is set up to help. – rdnewman Sep 18 '15 at 23:03
  • Thanks, I indeed use Rails 4 and the form is tied to a model (added to OP). The `country` validation, is something I'd like at the form level and not at the model level (in other forms country is *not* required). – Marty Sep 19 '15 at 07:46

1 Answers1

1

make sure your model is having validations

class Model
  validates :country, presence: true
  validates :id_or_no_id
  private
  def id_or_no_id
    if #custom logic here#
      errors.add :some_field, "you must have a valid id or no id"   
    end
  end
end

edit

based on your edit, your function should look like this, for better coding (which is also ugly code)

validate  :id_or_no_id
def id_or_no_id
  if (id_number.nil? || id_number.empty?) && !no_id
    errors.add(:id_number, "Need id or need to select checkbox")
  elsif (id_number && id_number.present?) && no_id
    errors.add(:id_number, "Can't have both id and checked checkbox")
  end
end

maybe this is also working

validate :use_one_id

def use_one_id
  if !id_number && !no_id
    errors.add :id_number, "you must select one...."
  end

  if id_number && no_id
    errors.add :id_number, "can't select both"
  end
end
Tim Kretschmer
  • 2,272
  • 1
  • 22
  • 35
  • Thanks. The `country` validation I'd only like at the form level and not at the model level (in other forms country is *not* required). I've added the private method to my OP. – Marty Sep 19 '15 at 08:12
  • then you need to custom_validate_it. just write your own function for validation, where you can check if it needs to be neccessary or not – Tim Kretschmer Sep 19 '15 at 08:16
  • in gereral, having a model that sometimes needs a valid country or not, sounds like a bad rails approach, which is non the rails-style – Tim Kretschmer Sep 19 '15 at 08:17
  • The reason is a distinction between a free and paid account, where only for the paid account `country` is needed (is needed for the transaction). – Marty Sep 19 '15 at 08:19
  • Is it not possible to make `country` required for only this form, using `required: true`? I know someone could easily hack this if such requirement isn't also added to the model validations, but that's okay in this particular case. – Marty Sep 19 '15 at 08:19
  • a form-check-validation is crap and can easily be ignored. so even if you validate it in your form as required, and your model doenst, i can create the object with wrong validations – Tim Kretschmer Sep 19 '15 at 08:21
  • please read this and try to understand it: http://guides.rubyonrails.org/active_record_validations.html – Tim Kretschmer Sep 19 '15 at 08:22
  • Thanks, I'm not sure if a custom validation is possible here. I don't think it's possible to use an `if statement` based on the controller method that is saving the record. And there's are no model variables that indicate whether `country` is required or not. The fact that a form-check-validation can be ignored is no problem here. That would only be to the disadvantage of the user. – Marty Sep 19 '15 at 08:48
  • so, if you are sure, that a validation is not possible there, then please feel free to do what you want to do. – Tim Kretschmer Sep 19 '15 at 09:10
  • after seeing your edit, please read also http://stackoverflow.com/questions/5068304/in-ruby-when-should-you-use-self-in-your-classes – Tim Kretschmer Sep 19 '15 at 09:11
  • Thanks, that helps, I removed the `.self`. I've also updated the OP with the behavior for making `country` required at the form level, I don't seem to be able to get the `placeholder` and `required` to work simultaneously. – Marty Sep 19 '15 at 09:21
  • I got it to work using: `], { :include_blank => 'Please select location' }, :required => true` – Marty Sep 19 '15 at 09:44