Hi

I'm adding a custom validation for the first checkout page:

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'commerce_checkout_form_checkout') {
    $form['buttons']['continue']['#validate'][] = 'MYMODULE_custom_validate';
  }
}

function MYMODULE_custom_validate(&$form, &$form_state) {
  form_set_error('MYMODULE', 'Error string');
}

It seems that after receiving the error message through AJAX (because the Continue button is AJAX driven) the following CSS files aren't reloaded, resulting in a table layout glitch for the cart content display (the order total mini table now spans full width instead of 33%).

commerce/modules/cart/theme/commerce_cart.theme.css
commerce/modules/price/theme/commerce_price.theme.css

As a temporary workaround I reattached them programatically to the form:

function MYMODULE_custom_validate(&$form, &$form_state) {
  form_set_error('MYMODULE', 'Error string');
  $form['#attached']['css'][] = drupal_get_path('module', 'commerce_cart') . '/theme/commerce_cart.theme.css';
  $form['#attached']['css'][] = drupal_get_path('module', 'commerce_price') . '/theme/commerce_price.theme.css';
}

- Propaganistas

CommentFileSizeAuthor
#2 screenshot_broken_checkout_css.png39.94 KBAnonymous (not verified)
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Anonymous’s picture

Issue summary: View changes

grammar

rszrama’s picture

Version: 7.x-1.7 » 7.x-1.x-dev
Category: bug » support
Status: Active » Postponed (maintainer needs more info)

The continue button isn't actually triggered via AJAX, however if you do something like change a country, which does have an AJAX onchange event attached to it, then the form will validate as it's rebuilt. Can you post a screenshot of the form as you see it? And can we rule out just an incompatibility with the theme on your site?

Anonymous’s picture

Hi rszrama

I'm experiencing this as you can see in the attached screenshot on Drupal's default Bartik theme.
Just for the record: I'm comparing the quantity of each line item against a fixed value, so no complex validations going on...

Thanks.

haggins’s picture

I had the same problem. So I added the validation handler to the cart contents pane (as I need to validate the cart contents again after form submission):

/**
 * Implements hook_commerce_checkout_pane_info_alter().
 */
function mymodule_commerce_checkout_pane_info_alter(&$checkout_panes) {
  if (isset($checkout_panes['cart_contents'])) {
    // We just need to add our validation. However, the system requires to
    // add also the default callbacks:
    $checkout_panes['cart_contents']['callbacks'] = array(
      'checkout_form_validate' => 'mymodule_pane_cart_contents_validate',
      'settings_form' => 'commerce_cart_contents_pane_settings_form',
      'checkout_form' => 'commerce_cart_contents_pane_checkout_form',
      'checkout_form_submit' => 'commerce_cart_contents_pane_checkout_form_submit',
      'review' => 'commerce_cart_contents_pane_review',
    );
  }
}
rszrama’s picture

Why didn't you just add a validate handler to the Continue button?

haggins’s picture

I can't remember details as I had this problem a few weeks ago.
However, adding the validation handler to the Continue button was of course the first thing I did. As I took a look at the default validation handler I saw that it just calls the panes validation handlers. So I used this and all problems were gone.

Adding the validation handler to the Continue button resulted in broken styles if validation failed (see screenshot of #2).

Anonymous’s picture

Category: support » task
Status: Postponed (maintainer needs more info) » Needs work
Anonymous’s picture

Title: Custom form_set_error on checkout breaks CSS » CSS glitch after unsuccessful validation in Checkout
Anonymous’s picture

Issue summary: View changes

code cleanup

ficklecat’s picture

I am also having the same issue as #1 when using Kickstart 2.

Adding a custom validation handler to the continue button does not load the correct CSS files when changing the country field.

Attaching the CSS files manually did not work for me and the hook_commerce_checkout_pane_info_alter code in #3 loads the CSS OK but the checkout form does not advance to the next pane (no message why).

rszrama’s picture

Ok, I was able to reproduce this using the code in the original post. What was happening was the checkout pane was simply using drupal_add_css() instead of properly attaching its CSS to its form element in the first place. Thus when the form was rebuilt on a validation error, the CSS wasn't being attached (as expected).

It's an easy enough fix to just change the way we attach it.

Unfortunately, I can't use #attached in the display formatter output for the price field, so we will just have to make an accommodation and attach it to the pane as well. I don't really like that, because it means we open the door to attach any ol' CSS file to the form (and I'm not even sure how that plays with RTL support). But I don't see a way around that.

Commit: http://drupalcode.org/project/commerce.git/commitdiff/8122dd1

rszrama’s picture

Title: CSS glitch after unsuccessful validation in Checkout » The Cart pane does not properly attach its CSS to the checkout form
Category: Task » Bug report
Issue summary: View changes
Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.