Darkmouse
Darkmouse

Reputation: 1939

Rails - How to validate radio button and text field values?

I'm surprised that no one asked this question yet. I am trying to validate radio buttons, with values and then a custom button, in a form, where users can type in custom amounts.

Here is my form

<%= f.radio_button(:price, 100)
<%= f.label(:price, "$100"</br>
<%= f.radio_button(:price, 200)
<%= f.label(:price, "$200"</br>
<%= f.radio_button(:price, 300)
<%= f.label(:price, "$300"</br>
<%= f.radio_button(:price, :price)
<%= f.label(:price, "Custom:"</br>
<%= f.text_field(:price) %>

The form will look like this

O $100

O $200

O $300

O Custom _________________

Anyways, I wrote this validation method in my model

validates :price, inclusion: { in: [*100..1000] } 

The datatype of :price is a decimal.

The form validates, when I type in text_field values, but not when I click on a radio button. Am I missing something? validates_inclusion should validate whether a value is in a range. All 3 radio button values (100, 200, 300) are in the range 100..1000. I do not understand why this doesn't work.

When I submit the values from the radio buttons, the parameter is not being saved. It is saved when I submit values from the text field though.

Here is a similar question

Rails 4 radio button form helper, true not validating

They said to use inclusion, but I had already tried it and it didn't work.

Upvotes: 0

Views: 1932

Answers (3)

Ken Stipek
Ken Stipek

Reputation: 1522

You should be able to do this, but you should be using integer to store price, not decimal. Use this method t convert either decimal, integer, or string values to be saved in your DB as pennies.

def price=(val)
  val.gsub!(',', '') if val.kind_of? String
  if !!(val =~ /\.+/)
    write_attribute(:price, (val.to_f * 100).to_i)
  elsif val.to_i.to_s == val
    write_attribute(:price, val.to_i)
  else
    write_attribute(:price, val)
  end
end

You then should be able to do this:

validates :price,   numericality: { greater_than_or_equal_to: 100, 
                                    less_than_or_equal_to: 1000,
                                    message: 'not a valid price' }

Source: I have this code working for me in a project right now.

Upvotes: 1

Darkmouse
Darkmouse

Reputation: 1939

Unfortunately, it turns out that there is no way to do this. I tested it out, by removing the text-field tag and selecting only radio buttons. The values were then saved. Adding the text field back, the values were "nil" when selecting radio buttons, because the field for amount was empty.

The reason was I had two separate fields for the :amount attribute. There was one way to solve this though.

rails g migration AddCustomToProducts custom_amount:string     # Need to create a separate field
rake db:migrate                                                # Migrate database

You could then set up your fields as follows

<%= f.radio_button(:price, 100)
<%= f.label(:price, "$100"</br>

<%= f.radio_button(:price, 200)
<%= f.label(:price, "$200"</br>

<%= f.radio_button(:price, 300)
<%= f.label(:price, "$300"</br>

<%= f.label(:custom_price, "Custom:"</br>
<%= f.text_field(:custom_price) %>

With this, I solved my problem.

Upvotes: 0

thaleshcv
thaleshcv

Reputation: 752

did you tried with numericality validator?

validates :price,
    numericality: {
        greater_than_or_equal_to: 100,
        less_than_or_equal_to: 1000,
        only_integer: true # or false to accept decimals
    }

Upvotes: 1

Related Questions