You are here

function range_field_widget_validate_subelement in Range 7

FAPI validation of a FROM or TO range subelement.

1 call to range_field_widget_validate_subelement()
range_field_widget_validate in ./range.module
FAPI validation of an individual range element.

File

./range.module, line 714
Defines range field types.

Code

function range_field_widget_validate_subelement($subelement, $element, &$form_state) {
  $value = $element[$subelement]['#value'];
  if ($value === '') {
    return;
  }
  $field = field_widget_field($element, $form_state);
  $type = $element['#range_type'];
  $valid = TRUE;
  if ($type === 'integer') {
    $valid = filter_var($value, FILTER_VALIDATE_INT);
    $message_args = array(
      '%field' => $element[$subelement]['#title'],
    );
    $message = t('Only numbers are allowed in %field.', $message_args);
  }
  else {
    $options = array(
      'options' => array(
        'decimal' => $field['settings']['decimal_separator'],
      ),
    );
    $valid = filter_var($value, FILTER_VALIDATE_FLOAT, $options);
    $message_args = array(
      '%field' => $element[$subelement]['#title'],
      '@separator' => $field['settings']['decimal_separator'],
    );
    $message = t('Only numbers and one decimal separator (@separator) allowed in %field.', $message_args);
  }
  if ($valid === FALSE) {
    form_error($element[$subelement], $message);
  }
  else {
    if ($type === 'decimal' || $type === 'float') {
      $value = strtr($value, $field['settings']['decimal_separator'], '.');
    }
    form_set_value($element[$subelement], $value, $form_state);
  }
}