Validation Errors

Vaildation errors will be returned on error result objects when we cannot process the API call because parameters are invalid. Validation errors have a code, message, and attribute. The code is intended for programmatic consumption, the message is human-readable, and the attribute identified which parameter the validation error is on.

Hierarchy

Errors are returned in a hierarchy that matches the parameter hierarchy. For example, when creating a customer with a credit card and billing address, the credit card is nested under customer, and the billing address under credit card.

result = Braintree::Customer.create(
  :email => "invalid_email",
  :credit_card => {
    :number => "not_numeric",
    :billing_address => {
      :country_name => "not_a_valid_country"
    }
  }
)

You can get all errors at all levels, errors at a specific level, or errors at a specific level on a specific attribute.

All Errors on All Levels

To get all errors on all levels, call each on result.errors. You can also check result.errors.size for the number of total errors.

result.errors.each do |error|
  puts error.code
  puts error.message
end

Errors at Specific Levels

To get errors at a specific level, use the for method. The attribute will indicate which attribute is invalid.

result.errors.for(:customer).each do |error|
  puts error.attribute
  puts error.code
  puts error.message
end

result.errors.for(:customer).for(:credit_card).each do |error|
  puts error.attribute
  puts error.code
  puts error.message
end

result.errors.for(:customer).for(:credit_card).for(:billing_address).each do |error|
  puts error.attribute
  puts error.code
  puts error.message
end

From a specific level, you can also get the number of errors at that level using size.

# number of errors on customer
result.errors.for(:customer).size

# number of errors on credit card
result.errors.for(:customer).for(:credit_card).size

# number of errors on billing address
result.errors.for(:customer).for(:credit_card).for(:billing_address).size

Errors on Specific Attribute

You can also get errors at a specific level on a specific attribute. This is useful if you want to display error messages inline in your forms.

result.errors.for(:customer).on(:email)
result.errors.for(:customer).for(:credit_card).on(:number)
result.errors.for(:customer).for(:credit_card).for(:billing_address).on(:country_name)

Base Errors

Sometimes validation errors aren’t caused by a specific input parameter. For example, canceled subscriptions cannot be updated. For these validation errors, we use an attribute named “base” for the validation error.

result = Braintree::Subscription.update(
  "the_subscription_id",
  :price => "10.00"
)
result.errors.for(:subscription).on(:base).each do |error|
  puts error.message
end

Errors on Add-ons/Discounts

It is possible to add, update and remove many add-ons and discounts at once. If any of the add-ons or discounts contain errors, these errors will be indexed based on the order of the add-on or discount in the request (begining at 0).

result = Braintree::Subscription.create(
  :payment_method_token => "the_payment_method_token",
  :plan_id => "the_plan_id",
  :add_ons => {
    :update => [
      {
        :existing_id => "add_on_id_1",
        :amount => "invalid"
      },
      {
        :existing_id => "add_on_id_2",
        :quantity => -10,
      }
    ]
  }
)

result.errors.for(:subscription).for(:add_ons).for(:update).for_index(0).on(:amount).each do |error|
  puts error.message
end

result.errors.for(:subscription).for(:add_ons).for(:update).for_index(1).on(:quantity).each do |error|
  puts error.message
end

See Also