You are here

function commerce_pricing_attributes_widget_validate in Commerce Pricing Attributes 7

Form validation handler for commerce_pricing_attributes_field_widget_form().

1 string reference to 'commerce_pricing_attributes_widget_validate'
commerce_pricing_attributes_field_widget_form in ./commerce_pricing_attributes.module
Implements hook_field_widget_form().

File

./commerce_pricing_attributes.module, line 626

Code

function commerce_pricing_attributes_widget_validate($element, &$form_state) {

  // If set_id is not empty.
  if (!empty($element['set_id']['#value'])) {
    $enabled_fields = 0;

    // If set_details is not empty.
    if (!empty($element['set_details'])) {

      // Iterate through the fields of the option set.
      foreach (element_children($element['set_details']) as $field_name) {

        // If field is enabled increment count variable.
        if ($element['set_details'][$field_name]['enabled']['#value']) {
          $enabled_fields++;
        }

        // If field options exists then validate.
        if (!empty($element['set_details'][$field_name]['options'])) {
          foreach (element_children($element['set_details'][$field_name]['options']) as $option) {
            if ($option === '_none') {
              continue;
            }

            // An option can not be disables and deafult value at the same time.
            if (!$element['set_details'][$field_name]['options'][$option]['enabled']['#value'] and ($element['set_details'][$field_name]['options'][$option]['default']['#value'] == $option and $element['set_details'][$field_name]['options'][$option]['default']['#type'] == 'radio' or $element['set_details'][$field_name]['options'][$option]['default']['#value'] == 1 and $element['set_details'][$field_name]['options'][$option]['default']['#type'] == 'checkbox')) {
              form_error($element['set_id'], t('You have selected as default value of the option set "%option_set" a disabled option. Please enable the option or change the default option.', array(
                '%option_set' => $element['set_id']['#options'][$element['set_id']['#value']],
              )));
            }

            // Price value must be numeric.
            if (!is_numeric($element['set_details'][$field_name]['options'][$option]['price']['#value'])) {
              form_error($element['set_details'][$field_name]['options'][$option]['price'], t('%title: you must enter a numeric value for the price amount.', array(
                '%title' => $element['set_id']['#options'][$element['set_id']['#value']],
              )));
            }
            else {
              form_set_value($element['set_details'][$field_name]['options'][$option]['price'], commerce_currency_decimal_to_amount($element['set_details'][$field_name]['options'][$option]['price']['#value'], $element['set_details'][$field_name]['options'][$option]['currency_code']['#value']), $form_state);
            }
          }
        }
      }
    }

    // If all fields are disabled then throw an error.
    if (!$enabled_fields) {
      form_error($element['set_id'], t('Option set "%option_set" must have at least one field eneabled.', array(
        '%option_set' => $element['set_id']['#options'][$element['set_id']['#value']],
      )));
    }
  }
}