You are here

function partial_date_field_estimates_validate_parse_options in Partial Date 7

Validate and parse the options. This is fairly loose validation, empty values will be cast to 0.

1 string reference to 'partial_date_field_estimates_validate_parse_options'
partial_date_field_estimates_settings_form in ./partial_date.admin.inc
Helper function to duplicate the same settings on both the instance and field settings.

File

./partial_date.admin.inc, line 177
Less freq. functions for field administration.

Code

function partial_date_field_estimates_validate_parse_options(&$element, &$form_state) {
  $items = array();
  foreach (explode("\n", $element['#value']) as $line) {
    $line = trim($line);
    if (!empty($line)) {
      list($from, $to, $label) = explode('|', $line . '||');
      if (!strlen($from) && !strlen($to)) {
        continue;
      }
      $label = trim($label);
      if (empty($label)) {
        form_error($element, t('The label for the keys %keys is required.', array(
          '%keys' => $from . '|' . $to,
        )));
      }
      elseif (!is_numeric($from) || !is_numeric($to)) {
        form_error($element, t('The keys %from and %to must both be numeric.', array(
          '%from' => $from,
          '%to' => $to,
        )));
      }
      else {

        // We need to preserve empty strings, so cast to temp variables.
        $_from = (int) $from;
        $_to = (int) $to;
        $out_of_range = FALSE;
        $limits = array(
          'month' => 12,
          'day' => 31,
          'hour' => 23,
          'minute' => 59,
          'second' => 59,
        );
        if (isset($limits[$element['#date_component']])) {
          $limit = $limits[$element['#date_component']];
          if ($_to > $limit || $_to < 0 || $_from > $limit || $_from < 0) {
            form_error($element, t('The keys %from and %to must be within the range 0 to !max.', array(
              '%from' => $_from,
              '%to' => $_to,
              '!max' => $limit,
            )));
            continue;
          }
        }
        $items[$from . '|' . $to] = $label;
      }
    }
  }

  // Do we need to check?
  if (!form_get_error($element)) {
    form_set_value($element, $items, $form_state);
  }
}