You are here

function commerce_price_table_field_validate in Commerce Price Table 7

Implements hook_field_validate().

File

./commerce_price_table.module, line 193

Code

function commerce_price_table_field_validate($entity_type, $entity, $field, $instance, $langcode, &$items, &$errors) {

  // Ensure only numeric values are entered in price fields.
  foreach ($items as $delta => $item) {

    // If the current item's price is not set, skip validating its row.
    if (!isset($item['amount']) || $item['amount'] == '') {
      continue;
    }
    if (!empty($item['amount']) && !is_numeric($item['amount'])) {
      $errors[$field['field_name']][$langcode][$delta][] = array(
        'error' => 'price_numeric',
        'message' => t('%name: you must enter a numeric value for the price.', array(
          '%name' => $instance['label'],
        )),
      );
    }

    // Ensure the quantity fields are valid values.
    if (!isset($item['min_qty']) || $item['min_qty'] == '' || !ctype_digit($item['min_qty']) || $item['min_qty'] < 1) {
      $errors[$field['field_name']][$langcode][$delta][] = array(
        'error' => 'price_table_min_qty',
        'message' => t('%name: Minimum quantity values must be integers greater than 0.', array(
          '%name' => $instance['label'],
        )),
      );
    }
    if (!isset($item['max_qty']) || $item['max_qty'] == '' || !ctype_digit($item['max_qty']) && $item['max_qty'] != -1 || $item['max_qty'] < -1 || $item['max_qty'] == 0) {
      $errors[$field['field_name']][$langcode][$delta][] = array(
        'error' => 'price_table_max_qty',
        'message' => t('%name: Maximum quantity values must be integers greater than 0 or -1 for unlimited.', array(
          '%name' => $instance['label'],
        )),
      );
    }
    if ($item['max_qty'] < $item['min_qty'] && $item['max_qty'] != -1) {
      $errors[$field['field_name']][$langcode][$delta][] = array(
        'error' => 'price_table_max_qty',
        'message' => t('%name: Maximum quantity values must be higher than their related minimum quantity values.', array(
          '%name' => $instance['label'],
        )),
      );
    }

    // @TODO Add extra validations, as no repeating qty and always force to have quantity for 1?.
  }
}