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

Jurgen8en’s picture

It works :-)

I changed the validation call:

    $form['customer_profile_billing']['commerce_customer_address']['und'][0]['locality_block']['#element_validate'][] = 'custom_postal_code_form_validate_billing';
    $form['customer_profile_shipping']['commerce_customer_address']['und'][0]['locality_block']['#element_validate'][] = 'custom_postal_code_form_validate_shipping';
function custom_postal_code_form_validate_billing($element, &$form_state, $form){
  if (!empty($element['postal_code']['#value'])) {
    $new_postal_code = _custom_postal_code_validate($form['customer_profile_billing'], $form_state['values']['customer_profile_billing']);
    form_set_value($element['postal_code'], $new_postal_code, $form_state);
  }
}

function custom_postal_code_form_validate_shipping($element, &$form_state, $form){
  if (!empty($element['postal_code']['#value'])) {
    $shipping_copy = $form_state['values']['customer_profile_shipping']['commerce_customer_profile_copy'];
    if(!$shipping_copy){
      $new_postal_code = _custom_postal_code_validate($form['customer_profile_shipping'], $form_state['values']['customer_profile_shipping']);
      form_set_value($element['postal_code'], $new_postal_code, $form_state);
    }
  }
}
Liam Morland’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

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