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,
(This post has some documentation about using this module with Drupal Commerce checkout form.)
A nice feature of this module is that is generates a nicer postal code: ex. for Austria '8999996' -> '8999-996'.
But, how can I update this nice postal code in the validate-function?
My best try, doesn´t change the postal code on review screen:
$element['#parents'] = array('customer_profile_billing','commerce_customer_address', 'und', 0, 'postal_code');
form_set_value($element, $new_postal_code, $form_state);
DOCUMENTATION
I added a validation function in custom_form_commerce_checkout_form_checkout_alter()
$form['buttons']['continue']['#validate'][] = 'custom_postal_code_form_validate';
Validate function:
function custom_postal_code_form_validate($form, &$form_state) {
//billing
$new_postal_code = _custom_postal_code_validate($form['customer_profile_billing'], $form_state['values']['customer_profile_billing']);
/***How to update***/
//shipping
$shipping_copy = $form_state['values']['customer_profile_shipping']['commerce_customer_profile_copy'];
if(!$shipping_copy){
_custom_postal_code_validate($form['customer_profile_shipping'], $form_state['values']['customer_profile_shipping']);
}
}
Helper:
function _custom_postal_code_validate($form_child, &$form_state_child){
$form_postal = $form_child['commerce_customer_address']['und'][0]['locality_block']['postal_code'];
$postal_code = $form_state_child['commerce_customer_address']['und'][0]['postal_code'];
$country = $form_state_child['commerce_customer_address']['und'][0]['country'];
//Regex Validate
$regex_validate = postal_code_validation_validate($postal_code, $country);
if(!empty($regex_validate['error'])){
form_error($form_postal, $regex_validate['error']);
}
if(!empty($regex_validate['postal_code'])){
return $regex_validate['postal_code'];
}
return $postal_code
}
Comments
Comment #1
Jurgen8en CreditAttribution: Jurgen8en commentedIt works :-)
I changed the validation call:
Comment #2
Liam Morland