You are here

function commerce_fedex_settings_form_validate in Commerce FedEx 7

Validate handler for FedEx admin settings form.

Parameters

array $form: The FedEx admin settings form.

array $form_state: The FedEx admin settings form state.

File

includes/commerce_fedex.admin.inc, line 327
Admin functions for Commerce FedEx.

Code

function commerce_fedex_settings_form_validate($form, $form_state) {

  // Define numeric fields that should be validated.
  $numeric_fields = array(
    'commerce_fedex_default_package_size_length' => t('length'),
    'commerce_fedex_default_package_size_width' => t('width'),
    'commerce_fedex_default_package_size_height' => t('height'),
  );

  // Validate any numeric fields within the submitted values.
  foreach ($numeric_fields as $key => $numeric_field) {
    if (!is_numeric($form_state['values'][$key]) || $form_state['values'][$key] < 1) {
      form_set_error($key, t('Value for @field must be a positive number.', array(
        '@field' => $numeric_field,
      )));
    }
  }
  if ($form_state['values']['commerce_fedex_services']['SMART_POST'] == 'SMART_POST') {
    foreach (array(
      'commerce_fedex_smartpost_hub_id',
      'commerce_fedex_smartpost_indicia_type',
    ) as $element) {
      if ($form_state['values'][$element] == '') {
        form_set_error($element, t('Please enter a value to use FedEx SmartPost.'));
      }
    }
  }
  if (count(array_intersect($form_state['values']['commerce_fedex_services'], array(
    'FEDEX_GROUND',
    'GROUND_HOME_DELIVERY',
  ))) && $form_state['values']['commerce_fedex_default_package_type'] != 'YOUR_PACKAGING') {
    form_set_error('commerce_fedex_default_package_type', t('To use a Ground service, you must select <em>Customer Supplied Packaging</em>.'));
  }
  if ("" == trim($form_state['values']['commerce_fedex_shipper_name']) && "" == trim($form_state['values']['commerce_fedex_company_name'])) {
    form_set_error('commerce_fedex_shipper_name', t('You must enter one or both of the Person name or Company name fields.'));
  }

  // Validate FedEx settings that are required from USA and Canada only.
  if (in_array($form_state['values']['commerce_fedex_country_code'], array(
    'US',
    'CA',
  ))) {

    // Make sure state is provided.
    if (empty($form_state['values']['commerce_fedex_state'])) {
      form_set_error('commerce_fedex_state', t('State or Province is required when shipping from USA or Canada.'));
    }
    else {

      // Make sure state is 2 characters.
      if (!preg_match('/^[a-z][a-z]$/i', $form_state['values']['commerce_fedex_state'])) {
        form_set_error('commerce_fedex_state', t('Please enter the 2 character abbreviation for your State or Province'));
      }
    }

    // Make sure postal code is provided.
    if (empty($form_state['values']['commerce_fedex_postal_code'])) {
      form_set_error('commerce_fedex_postal_code', t('Postal code is required when shipping from USA or Canada.'));
    }
  }
}