From 3a150be2f38379e15d87ee521e1b390464ece1c0 Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Sat, 26 Aug 2017 19:01:42 +1000 Subject: [PATCH] test ajax validation of early access code --- booking.regn_form.inc | 41 +++++++++++++++++++++++++++++++++++++++-- booking.rooms.inc | 24 ++++++++++++------------ 2 files changed, 51 insertions(+), 14 deletions(-) diff --git a/booking.regn_form.inc b/booking.regn_form.inc index b2b47cc..17eb45e 100644 --- a/booking.regn_form.inc +++ b/booking.regn_form.inc @@ -111,6 +111,9 @@ function booking_form($node, &$form_state, $inserting = FALSE, $early_access_all '#type' => 'fieldset', '#title' => 'Early Registration Code' ); + $form['early-access']['booking_earlyaccess_feedback_wrapper'] = array( + '#markup' => '
', + ); $form['early-access']['booking_earlyaccess_explanation'] = array( '#type' => 'container', '#children' => t("

If you have been given an early-access code for registration, please enter it below. " . @@ -122,7 +125,12 @@ function booking_form($node, &$form_state, $inserting = FALSE, $early_access_all '#title' => t('Unique Early-Access Code'), '#maxlength' => 45, '#required' => TRUE, - '#default_value' => !empty($data->booking_earlyaccess_code) ? $data->booking_earlyaccess_code : '' + '#default_value' => !empty($data->booking_earlyaccess_code) ? $data->booking_earlyaccess_code : '', + '#ajax' => array( + 'event' => 'change', + 'wrapper' => 'booking_earlyaccess_feedback_wrapper', + 'callback' => 'booking_earlyaccess_feedback_callback', + ), ); $form['booking_earlyaccess'] = array( '#type' => 'hidden', @@ -930,7 +938,36 @@ function booking_form($node, &$form_state, $inserting = FALSE, $early_access_all return $form; } -// @todo - validate the early access code here, booking_earlyaccess_code +function booking_earlyaccess_feedback_callback($form, &$form_state) { + global $event; + $node = $form_state['values']['form_id']; + $data = $form_state['input']; + + //if necessary, validate early access code + if (variable_get('booking_enable_earlyaccess_codes', 0) == 1 && (isset($data['booking_earlyaccess']) && $data['booking_earlyaccess'] == 1)) { + if (isset($data['booking_earlyaccess_code']) && $data['booking_earlyaccess_code'] != '') { + //perform a database lookup against the booking_earlyaccess_codes table + $code_check = db_query("SELECT cid " . + "FROM {booking_earlyaccess_codes} " . + "WHERE booking_earlyaccess_code = :code AND booking_eventid = :eid AND booking_earlyaccess_code_avail = 'Y'", + array( + ':code' => $data['booking_earlyaccess_code'], + ':eid' => $event->eid, + ) + )->fetchObject(); + //see if we got a result + if (! $code_check) { + return '

You have entered an invalid early registration code. If you do not have a code, please wait until normal registrations open.
'; + } + } + else { + return '
You must enter an early access code to register now. If you do not have a code, please wait until normal registrations open.
'; + } + } + + #fallback + return '
'; +} function booking_form_validate($form, &$form_state) { global $event; diff --git a/booking.rooms.inc b/booking.rooms.inc index 8a38cd8..bbdd579 100644 --- a/booking.rooms.inc +++ b/booking.rooms.inc @@ -139,11 +139,11 @@ function booking_room_edit_form($node, &$form_state, $nid) { '#title' => t('Room Location'), '#options' => $location_options, '#default_value' => $person->booking_room_location_id, - '#ajax' => array( - 'event' => 'change', - 'wrapper' => 'booking_roomnum_wrapper', - 'callback' => 'booking_roomnum_ajax_callback', - ), + '#ajax' => array( + 'event' => 'change', + 'wrapper' => 'booking_roomnum_wrapper', + 'callback' => 'booking_roomnum_ajax_callback', + ), ); $form['booking_room_number'] = array( @@ -154,18 +154,18 @@ function booking_room_edit_form($node, &$form_state, $nid) { '#suffix' => '', '#options' => _booking_get_roomedit_roomnum_options($selected_room_location), '#default_value' => isset($form_state['values']['booking_room_number']) ? $form_state['values']['booking_room_number'] : $person->booking_room_number, - '#ajax' => array( - 'event' => 'change', - 'wrapper' => 'booking_bedtype_wrapper', - 'callback' => 'booking_bedtype_ajax_callback', - ), + '#ajax' => array( + 'event' => 'change', + 'wrapper' => 'booking_bedtype_wrapper', + 'callback' => 'booking_bedtype_ajax_callback', + ), ); $form['booking_room_bedtype'] = array( '#type' => 'select', '#title' => t('Bed Type'), - '#prefix' => '
', - '#suffix' => '
', + '#prefix' => '
', + '#suffix' => '
', '#options' => _booking_get_roomedit_bedtype_options($selected_room_location, $selected_room_num), '#default_value' => $person->booking_room_bedtype, );