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(array(
  'email' => 'invalid',
  'creditCard' => array(
    'number' => 'invalid',
    'billingAddress' => array(
      'countryName' => '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 sizeof($result->errors) for the number of total errors.

foreach($result->errors->deepAll() AS $error) {
  print_r($error->code . ": " . $error->message . "\n");
}

Errors at Specific Levels

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

foreach($result->errors->forKey('customer')->shallowAll() AS $error) {
    print_r($error->attribute . ": " . $error->code . " " . $error->message . "\n");
}

foreach($result->errors->forKey('customer')->forKey('creditCard')->shallowAll() AS $error) {
    print_r($error->attribute . ": " . $error->code . " " . $error->message . "\n");
}

foreach($result->errors->forKey('customer')->forKey('creditCard')->forKey('billingAddress')->shallowAll() AS $error) {
    print_r($error->attribute . ": " . $error->code . " " . $error->message . "\n");
}

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

sizeof($result->errors->forKey('customer')->shallowAll())
# Number of errors only on customer

sizeof($result->errors->forKey('customer')->forKey('creditCard')->shallowAll())
# Number of errors only on credit card

sizeof($result->errors->forKey('customer')->forKey('creditCard')->forKey('billingAddress')->shallowAll())
# Number of errors only on billing address

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->forKey('customer')->onAttribute('email')
$result->errors->forKey('customer')->forKey('creditCard')->onAttribute('number')
$result->errors->forKey('customer')->forKey('creditCard')->forKey('billingAddress')->onAttribute('countryName')

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', array('price' => '1.00'));
$baseErrors = $result->errors->forKey('subscription')->onAttribute('base');

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(array(
    'paymentMethodToken' => 'the_payment_method_token',
    'planId' => 'the_plan_id',
    'addOns' => array(
        'update' => array(
            array(
                'existingId' => 'increase_10',
                'amount' => 'invalid',
            ),
            array(
                'existingId' => 'increase_20',
                'quantity' => -10,
            )
        )
    )
));

$amountErrors = $result->errors->forKey('subscription')->forKey('addOns')->forKey('update')->forIndex(0)->onAttribute('amount');
foreach($amountErrors AS $error) {
    print_r($error->code . ": " . $error->message . "\n");
}

$quantityErrors = $result->errors->forKey('subscription')->forKey('addOns')->forKey('update')->forIndex(1)->onAttribute('quantity');
foreach($quantityErrors AS $error) {
    print_r($error->code . ": " . $error->message . "\n");
}

See Also