You are here

public function CommerceProductInlineEntityFormController::entityFormValidate in Inline Entity Form 7

Overrides EntityInlineEntityFormController::entityFormValidate().

Overrides EntityInlineEntityFormController::entityFormValidate

File

includes/commerce_product.inline_entity_form.inc, line 259
Defines the inline entity form controller for Commerce Products.

Class

CommerceProductInlineEntityFormController
@file Defines the inline entity form controller for Commerce Products.

Code

public function entityFormValidate($entity_form, &$form_state) {
  $product = $entity_form['#entity'];
  $parents_path = implode('][', $entity_form['#parents']);
  $product_values = drupal_array_get_nested_value($form_state['values'], $entity_form['#parents']);
  $sku = trim($product_values['sku']);

  // If the SKU field is enabled, validate it.
  if (empty($entity_form['sku']['#disabled'])) {

    // Ensure the proposed SKU is unique.
    if (!commerce_product_validate_sku_unique($sku, $product->product_id)) {
      form_set_error($parents_path . '][sku', t('This SKU is already in use and must be unique. Please supply another value.'));
    }

    // Validate the SKU for invalid characters.
    if (!commerce_product_validate_sku($sku)) {
      form_set_error($parents_path . '][sku', t('The SKU %sku contains invalid characters.', array(
        '%sku' => $sku,
      )));
    }
  }

  // Trim leading and trailing whitespace from the SKU.
  drupal_array_set_nested_value($form_state['values'], array_merge($entity_form['#parents'], array(
    'sku',
  )), $sku);
  field_attach_form_validate('commerce_product', $product, $entity_form, $form_state);
}