You are here

function commerce_file_limit_integer_element_validate in Commerce File 7

FAPI element validate callback for limit textfield element type.

1 string reference to 'commerce_file_limit_integer_element_validate'
commerce_file_element_info in includes/commerce_file.elements.inc
Implements hook_element_info().

File

includes/commerce_file.elements.inc, line 136
Commerce File form elements

Code

function commerce_file_limit_integer_element_validate($element, &$form_state) {
  $data = $element['#value'];
  $value = NULL;
  if (empty($data)) {
    return;
  }
  if ($data['mode'] == 'value') {

    // process entered value
    $value = $data['value'];

    // validate as not empty here since #required on value would require for all modes
    if (_commerce_file_element_value_is_empty($value)) {
      form_error($element, t('%name field is required.', array(
        '%name' => $element['#title'],
      )));
      return;
    }

    // validate as positive integer or 0
    if (!_commerce_file_is_integer_or_zero($value)) {
      form_error($element, t('%name must be a positive integer or zero.', array(
        '%name' => $element['#title'],
      )));
      return;
    }

    // ensure integer value stored
    $value = intval($value);
  }
  else {

    // set value to the mode key
    $value = $data['mode'];
  }

  // Consolidate into one value.
  form_set_value($element, $value, $form_state);
}