Topic: Validate that the user selects a State from a dropdown

Here is the dropdown I'm using:

<%= select(:user, :state, [     
    ['Select a State', 'None'],
    ['Alabama', 'AL'],
    ['Alaska', 'AK'],
    ['Arizona', 'AZ'],
    ['Arkansas', 'AR'],
    ['California', 'CA'],
    ['Colorado', 'CO'],
    ['Connecticut', 'CT'],
    ['Delaware', 'DE'],
    ['District Of Columbia', 'DC'],
    ['Florida', 'FL'],
    ['Georgia', 'GA'],
    ['Hawaii', 'HI'],
    ['Idaho', 'ID'],
    ['Illinois', 'IL'],
    ['Indiana', 'IN'],
    ['Iowa', 'IA'],
    ['Kansas', 'KS'],
    ['Kentucky', 'KY'],
    ['Louisiana', 'LA'],
    ['Maine', 'ME'],
    ['Maryland', 'MD'],
    ['Massachusetts', 'MA'],
    ['Michigan', 'MI'],
    ['Minnesota', 'MN'],
    ['Mississippi', 'MS'],
    ['Missouri', 'MO'],
    ['Montana', 'MT'],
    ['Nebraska', 'NE'],
    ['Nevada', 'NV'],
    ['New Hampshire', 'NH'],
    ['New Jersey', 'NJ'],
    ['New Mexico', 'NM'],
    ['New York', 'NY'],
    ['North Carolina', 'NC'],
    ['North Dakota', 'ND'],
    ['Ohio', 'OH'],
    ['Oklahoma', 'OK'],
    ['Oregon', 'OR'],
    ['Pennsylvania', 'PA'],
    ['Rhode Island', 'RI'],
    ['South Carolina', 'SC'],
    ['South Dakota', 'SD'],
    ['Tennessee', 'TN'],
    ['Texas', 'TX'],
    ['Utah', 'UT'],
    ['Vermont', 'VT'],
    ['Virginia', 'VA'],
    ['Washington', 'WA'],
    ['West Virginia', 'WV'],
    ['Wisconsin', 'WI'],
    ['Wyoming', 'WY']]) %>

And my user model:
class User < ActiveRecord::Base
      validates_presence_of :nick
      validates_presence_of :password
      validates_presence_of :first_name
      validates_presence_of :last_name
      validates_presence_of :sex
      validates_presence_of :email
      validates_presence_of :city
      validates_presence_of :state
      has_and_belongs_to_many :events
    end

I want to display an error if the user doesn't select a State. That would mean the state value in my params hash is set to 'None'. So how do I check that they selected a value other than the default for the dropdown and display an error?

I suppose there is probably a "validates_non_noneness_of_state" method :-)

Last edited by Yak (2006-11-22 11:04:04)

Re: Validate that the user selects a State from a dropdown

you can add a validate method to the model somewhat like this:

def validate
  if state == 'None'
    errors.add_to_base("You must select a state")
  end
end

Re: Validate that the user selects a State from a dropdown

Cool, that's what I was looking for. A way to do some sort of custom validation and stuff the errors hash. So how/when/where do I call this method?

Re: Validate that the user selects a State from a dropdown

And you don't have specify the field your message relates to when you add it to the errors hash?

def validate
  if state == 'None'
    errors.add_to_base("You must select a state")
  end
end

Wouldn't this 'assign' the message to the state field?
def validate_state
  if state == 'None'
  errors.add(state,"You must select a state")
  end
end

Last edited by Yak (2006-11-22 12:28:02)

Re: Validate that the user selects a State from a dropdown

or remove the 'none' part. and just have validates_presence_of :state

--

Re: Validate that the user selects a State from a dropdown

Yak wrote:

Wouldn't this 'assign' the message to the state field?

def validate_state
  if state == 'None'
  errors.add(state,"You must select a state")
  end
end

Yeah, but make it a symbol:

def validate
  errors.add :state, "You must select a state" if state == 'None'
end

I would prefer making the "None" option an empty string though. Then you can just do the validates_presence_of.

Railscasts - Free Ruby on Rails Screencasts

Re: Validate that the user selects a State from a dropdown

Much better idea. I set the 'value' of the Select A State option to '' instead of 'None' and validates_presence_of picks that up. Thanks Ryan.