You are here

public function UCXF_Field::edit_form_validate in Extra Fields Checkout Pane 7

Same name and namespace in other branches
  1. 6.2 class/UCXF_Field.class.php \UCXF_Field::edit_form_validate()

Validate the edit form for the item.

_state @access public

Parameters

array $form:

Return value

void

File

class/UCXF_Field.class.php, line 528
Contains the UCXF_Field class.

Class

UCXF_Field
Base class for a Extra Fields Pane field

Code

public function edit_form_validate($form, &$form_state) {
  $field = $form_state['values']['ucxf'];

  // No label.
  if (!$field['label']) {
    form_set_error('ucxf][label', t('Custom order field: you need to provide a label.'));
  }

  // No field name.
  if (!$field['db_name']) {
    form_set_error('ucxf][db_name', t('Custom order field: you need to provide a field name.'));
  }
  if (isset($field['weight']) && !$field['weight'] && $field['weight'] !== 0 && $field['weight'] !== '0') {
    form_set_error('ucxf][weight', t('Custom order field: you need to provide a weight value for this extra field.'));
  }
  if (isset($form['ucxf']['pane_type']) && empty($field['pane_type'])) {
    form_set_error('ucxf][pane_type', t('Custom order field: you need to provide a pane-type for this extra field.'));
  }
  if (!$field['value_type']) {
    form_set_error('ucxf][value_type', t('Custom order field: you need to provide a way of processing the value for this field as either textbox, select, constant, or php.'));
  }
  if (($field['value_type'] == self::UCXF_WIDGET_TYPE_CONSTANT || $field['value_type'] == self::UCXF_WIDGET_TYPE_PHP) && !$field['value']) {
    form_set_error('ucxf][value', t('Custom order field: you need to provide a value for this way of calculating the field value.'));
  }

  // Field name validation.
  if (empty($field['field_id'])) {
    $field_name = $field['db_name'];

    // Add the 'ucxf_' prefix.
    if (strpos($field_name, 'ucxf_') !== 0) {
      $field_name = 'ucxf_' . $field_name;
      form_set_value($form['ucxf']['db_name'], $field_name, $form_state);
    }

    // Invalid field name.
    if (!preg_match('!^ucxf_[a-z0-9_]+$!', $field_name)) {
      form_set_error('ucxf][db_name', t('Custom order field: the field name %field_name is invalid. The name must include only lowercase unaccentuated letters, numbers, and underscores.', array(
        '%field_name' => $field_name,
      )));
    }

    // considering prefix ucxf_ no more than 32 characters (32 max for a db field)
    if (strlen($field_name) > 32) {
      form_set_error('ucxf][db_name', t('Custom order field: the field name %field_name is too long. The name is limited to !number characters, including the \'ucxf_\' prefix.', array(
        '!number' => 32,
        '%field_name' => $field_name,
      )));
    }

    // Check if field name already exists in table.
    $count = db_select('uc_extra_fields')
      ->condition('db_name', $field_name)
      ->countQuery()
      ->execute()
      ->fetchField();
    if ((int) $count > 0) {
      form_set_error('ucxf][db_name', t('Custom order field: the field name %field_name already exists.', array(
        '%field_name' => $field_name,
      )));
    }
  }

  // Check if php tags are present in case of a php field
  if ($field['value_type'] == self::UCXF_WIDGET_TYPE_PHP || $field['value_type'] == self::UCXF_WIDGET_TYPE_PHP_SELECT) {
    $php_open_tag_position = stripos($field['value'], '<?php');
    $php_close_tag_position = strripos($field['value'], '?>');
    if ($php_open_tag_position === FALSE || $php_close_tag_position === FALSE || $php_open_tag_position > $php_close_tag_position) {
      form_set_error('ucxf][value', t('The PHP code is not entered between %php.', array(
        '%php' => '<?php ?>',
      )));
      return;
    }
  }

  // Display a warning when select or PHP-select fields are marked as required, but have no option with an empty key
  if (($field['value_type'] == self::UCXF_WIDGET_TYPE_SELECT || $field['value_type'] == self::UCXF_WIDGET_TYPE_PHP_SELECT) && $field['required'] == TRUE) {
    $this->value_type = $field['value_type'];
    $this->value = $field['value'];
    $options = $this
      ->generate_value(FALSE);
    $has_empty_key = FALSE;
    foreach ($options as $key => $value) {
      if ($key === '' || $key === ' ') {
        $has_empty_key = TRUE;
        break;
      }
    }
    if (!$has_empty_key) {
      switch ($field['value_type']) {
        case self::UCXF_WIDGET_TYPE_SELECT:
          $message_suffix = t('In this example the key of the first item is just a single space.');
          break;
        case self::UCXF_WIDGET_TYPE_PHP_SELECT:
          $message_suffix = t('In this example the key of the first item is an empty string.');
          break;
      }
      drupal_set_message(t('The select field %field is marked as required, but there is no "empty" option in the list. Enter an empty option in the value section as in this example: !example', array(
        '%field' => $field['db_name'],
        '!example' => '<br />' . self::get_example($field['value_type']) . '<br />',
      )) . $message_suffix, 'warning');
    }
  }
}