You are here

function mvf_widget_validate in Measured Value Field 6

FAPI validation of an individual Measured Value field.

Validate "To" value only. "From" value is validated by Formatted Number validation function, because we reuse it's element.

1 string reference to 'mvf_widget_validate'
mvf_elements in ./mvf.module
Implementation of FAPI hook_elements().

File

./mvf.module, line 559
Measured Value Field module.

Code

function mvf_widget_validate($element, &$form_state) {
  $field_name = $element['#field_name'];
  $type_name = $element['#type_name'];
  $field = content_fields($field_name, $type_name);

  // When "To" value is disabled there's nothing to validate.
  if (empty($field['tovalue'])) {
    return;
  }
  $to_field = 'value2';
  $unit_field = 'unit';
  $value2 = trim($element['#value'][$to_field]);

  // Validate "To" value.
  if ($element[$field_key]['#required'] || $value2 != '') {
    $value2 = parse_formatted_number($value2, $element[$field_key]['#required']);
    if (!is_numeric($value2)) {
      $error_element = implode('][', $element['#parents']) . '][' . $to_field;
      form_set_error($error_element, t('The specified number !num is invalid.', array(
        '!num' => $element['#value'][$to_field],
      )));
      return;
    }
    $errors = formatted_number_validate_field_value($field, $value2);
    if (!empty($errors)) {
      $error_element = implode('][', $element['#parents']) . '][' . $to_field;
      foreach ($errors as $message) {
        form_set_error($error_element, $message);
      }
      return;
    }
  }

  // Update the form field with parsed number, so it gets a valid PHP number
  // that can be used to store in the database.
  if ($element['#value'][$to_field] != $value2) {
    form_set_value($element[$to_field], $value2, $form_state);
  }
}