nmp153
nmp153

Reputation: 13

Custom Validation for phone numbers in Rails

I am having some trouble with custom validation in rails. I want to validate that only numbers are being put into the field where they choose dial in the drop down list.

my code thus far

validate :validate_numericality_of_dial_arg

def validate_numericality_of_dial_arg
    if action_type == "dial"
      return action_dst =~ /^\d+$/
    else
      errors.add(:base, "must be numbers")
    end
end

right now I am getting the error to pop up saying must be numbers even if the user enters numbers

Upvotes: 1

Views: 325

Answers (1)

Mark
Mark

Reputation: 6455

If you don't want to insist on using a custom validator, you can always use some rails magic:

## app/model.rb
validates :action_dst, numericality: true, if: -> {action_type == "dial" }

Upvotes: 5

Related Questions