Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment | File | Size | Author |
---|---|---|---|
#2 | screenshot_broken_checkout_css.png | 39.94 KB | Anonymous (not verified) |
Comments
Comment #0.0
Anonymous (not verified) CreditAttribution: Anonymous commentedgrammar
Comment #1
rszrama CreditAttribution: rszrama commentedThe 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?
Comment #2
Anonymous (not verified) CreditAttribution: Anonymous commentedHi 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.
Comment #3
haggins CreditAttribution: haggins commentedI 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):
Comment #4
rszrama CreditAttribution: rszrama commentedWhy didn't you just add a validate handler to the Continue button?
Comment #5
haggins CreditAttribution: haggins commentedI 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).
Comment #6
Anonymous (not verified) CreditAttribution: Anonymous commentedComment #7
Anonymous (not verified) CreditAttribution: Anonymous commentedComment #7.0
Anonymous (not verified) CreditAttribution: Anonymous commentedcode cleanup
Comment #8
ficklecatI 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).
Comment #9
rszrama CreditAttribution: rszrama commentedOk, 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
Comment #10
rszrama CreditAttribution: rszrama commented