0

I'm using Stripe within a model class. I have a strike_token attribute that I am validating presences of and creating a custom message like so:

validates :stripe_token, presence: { message: 'Please enter valid card info' }

When the form is submitted and not valid, the error message is, "Stripe token Please enter valid card info"

I don't want Stripe token in the error message. I have other validations as well that would be suited for the default, so I don't want to change the default error behavior.

It would be better if I could alias the Stripe token attribute to something like :card_info without actually having that as an attribute on the model.

So the message could be made to read, "Card info cannot be blank"

Patrick
  • 1,410
  • 2
  • 19
  • 37

1 Answers1

4

You can do as given below:

Just remove the :message part from validation.

# config/locales/en.yml
en:
  activerecord:
    attributes:
      model_name:
        stripe_token: ""
    errors:
      models:
        model_name:
          attributes:
            stripe_token:
              blank: "Card info cannot be blank!"

OR

# config/locales/en.yml
en:
  activerecord:
    attributes:
      model_name:
        stripe_token: "Card info"
    errors:
      models:
        model_name:
          attributes:
            stripe_token:
              blank: " cannot be blank!"

Replace model_name with your model name in small letters. Please check Fully custom validation error message with Rails for more details.

Update

# config/locales/en.yml
    en:
      activerecord:
        attributes:
          model_name:
            stripe_token: "Card info"

Then use,

validates :stripe_token, presence: { message: ' cannot be blank!' }

Hope it helps :)

Community
  • 1
  • 1
Rajesh Omanakuttan
  • 6,788
  • 7
  • 47
  • 85
  • Changing the attribute name worked, but I would prefer to set the message on the model validation itself. Like `validates :stripe_token, presence: { message: 'cannot be blank!' }` Then I'd accept your answer – Patrick Apr 19 '14 at 00:27
  • Yes, I think that might be possible. Please see the update in my answer. – Rajesh Omanakuttan Apr 19 '14 at 06:31