function bat_event_autocomplete_validate in Booking and Availability Management Tools for Drupal 7
Validation callback for a event state autocomplete element.
1 string reference to 'bat_event_autocomplete_validate'
- bat_event_field_widget_form in modules/
bat_event/ bat_event.module - Implements hook_field_widget_form().
File
- modules/
bat_event/ bat_event.module, line 1946 - Manage Events - Events store the EventValue of a Unit over a period of time.
Code
function bat_event_autocomplete_validate($element, &$form_state, $form) {
$field = field_widget_field($element, $form_state);
$instance = field_widget_instance($element, $form_state);
$value = $element['#value'];
$state_id = NULL;
if (!empty($value)) {
// Check whether we have an explicit "[state_id:n]" input.
preg_match('/^(?:\\s*|(.*) )?\\[\\s*state_id\\s*:\\s*(\\d+)\\s*\\]$/', $value, $matches);
if (!empty($matches)) {
// Explicit state_id. Check that the 'title' part matches the actual title for
// the state_id.
list(, $title, $state_id) = $matches;
if (!empty($title)) {
$state = bat_event_load_state($state_id);
$real_title = $state['label'];
if (trim($title) != trim($real_title)) {
form_error($element, t('%name: title mismatch. Please check your selection.', array(
'%name' => $instance['label'],
)));
}
}
}
else {
// No explicit state_id (the submitted value was not populated by
// autocomplete selection). Get the state_id of a referenceable node from
// the entered title.
$options = array(
'string' => $value,
'match' => 'equals',
'limit' => 1,
);
$references = bat_event_potential_references($field, $options);
if ($references) {
// @todo The best thing would be to present the user with an
// additional form, allowing the user to choose between valid
// candidates with the same title. ATM, we pick the first
// matching candidate...
$state_id = key($references);
}
else {
form_error($element, t('%name: unable to find an event state with that title.', array(
'%name' => $instance['label'],
)));
}
}
}
// Set the element's value as the state id that was extracted from the entered
// input.
form_set_value($element, $state_id, $form_state);
}