You are here

function _office_hours_block_validate in Office Hours 7

Implements a callback for _office_hours_elements().

For 'office_hours_block' (day) and 'office_hours_select' (hour) elements. You can find the value in $element['#value'], but better in $form_state['values'], which is set in _office_hours_select_validate().

1 string reference to '_office_hours_block_validate'
office_hours_element_info in ./office_hours.module
Implements FAPI hook_element_info().

File

includes/office_hours.elements.inc, line 196
Office hours form elements and their theming and validation.

Code

function _office_hours_block_validate($element, &$form_state) {
  $item = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
  $error_text = '';
  $valhrs = $element['#field_settings']['valhrs'];
  $limitstart = $element['#field_settings']['limitstart'];
  $limitend = $element['#field_settings']['limitend'];
  $starthours = $item['starthours'];
  $endhours = $item['endhours'];

  // Do not use empty(), since '0' is a valid value, too.
  if ($starthours == '' xor $endhours == '') {
    $error_text = 'Both Opening hours and Closing hours must be set.';
  }
  elseif ($valhrs && $starthours > $endhours && $endhours != 0) {

    // Endhours can be '0' because we can close at midnight.
    $error_text = 'Closing hours are earlier than Opening hours.';
  }
  elseif (!empty($limitstart) || !empty($limitend)) {
    if ($starthours && $limitstart > $starthours || $endhours && $limitend < $endhours) {
      $error_text = 'Hours are outside limits ( !start - !end ).';
    }
  }
  if ($error_text) {
    $error_text = t($element['#dayname']) . ': ' . t($error_text, array(
      '!start' => _office_hours_time_to_24hr($limitstart),
      '!end' => _office_hours_time_to_24hr($limitend),
    ), array(
      'context' => 'office_hours',
    ));
    form_error($element, check_plain($error_text));
  }
}