You are here

function mvf_validate_field_value in Measured Value Field 6

Validate a single value (row) of measured value field and indicate errors.

Validates the unit and relation between the values and the unit. Individual numeric values are validated on widget level because widget provides specific number format.

Parameters

array $field: The field array.

array $delta: The field row delta in field array.

number $item: Array containing field row to validate

1 call to mvf_validate_field_value()
mvf_field in ./mvf.module
Implementation of hook_field().

File

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

Code

function mvf_validate_field_value($field, $delta, $item) {
  $value = $item['value'];
  $value2 = $item['value2'];
  $unit = $item['unit'];
  $widget_label = t($field['widget']['label']);
  $errors = array();
  $errors['unit'] = array();
  $errors['value'] = array();
  $errors['value2'] = array();

  // Validate values relationship.
  if ($field['tovalue']) {

    // "To" should be greater than "From".
    if (is_numeric($value2) && is_numeric($value) && $value2 <= $value) {
      $errors['value2'][] = t('%name: the "To" value must be greater than the "From" value.', array(
        '%name' => $widget_label,
      ));
    }

    // If the "To" value is filled in, the "From" one should be filled in too.
    if (is_numeric($value2) && !is_numeric($value)) {
      $errors['value'][] = t('%name: the "From" value must be filled in.', array(
        '%name' => $widget_label,
      ));
    }

    // If the "From" value is filled in, "To" one should be too, if it's required.
    // We need this check to validate additional (optional) rows in case of multiple values MVF.
    if (is_numeric($value) && !is_numeric($value2) && $field['tovalue'] == 'required') {
      $errors['value2'][] = t('%name: the "To" value must be filled in.', array(
        '%name' => $widget_label,
      ));
    }
  }

  // Validate units
  if (empty($unit)) {
    if ($field['required']) {
      $errors['unit'][] = t('%name: unit is required.', array(
        '%name' => $widget_label,
      ));
    }
    else {
      if (is_numeric($value) || is_numeric($value2)) {
        $errors['unit'][] = t('%name: unit is required when an value is specified.', array(
          '%name' => $widget_label,
        ));
      }
    }
  }
  else {

    // When validating the default value in field settings panel, CCK is giving
    // us the options at field level, not within the widget item of the field.
    if (!empty($field['allowed_units'])) {
      $allowed_units = isset($field['allowed_units']) ? array_filter($field['allowed_units']) : array();
      $value_required = FALSE;
    }
    else {
      $allowed_units = isset($field['widget']['allowed_units']) ? array_filter($field['widget']['allowed_units']) : array();
      $value_required = TRUE;
    }

    // When no unit is enabled, allow them all.
    if (empty($allowed_units)) {
      $allowed_units = units_get_unit_names();
    }
    if (!isset($allowed_units[$unit])) {
      if (!$field['required']) {
        $errors['unit'][] = t('%name: the unit %unit is not allowed.', array(
          '%name' => $widget_label,
          '%unit' => $unit,
        ));
      }
    }
    else {
      if (!is_numeric($value) && $value_required) {
        $errors[] = t('%name: a valid value is required when a unit is specified.', array(
          '%name' => $widget_label,
        ));
      }
    }
  }
  $error_field = $field['field_name'] . '][' . $delta . '][';
  foreach ($errors as $label => $error_messages) {
    if (is_array($error_messages)) {
      foreach ($error_messages as $message) {
        $error_element = $error_field . $label;
        form_set_error($error_element, $message);
      }
    }
  }
}