Run a boolean test function on form submit Is there a way to run a function when a user submits a form in Drupal that returns a true or false. If it is true, run the submission handler. If false, return an error.

Application: 

I am using Drupal to run a product booking system. If a product is reserved during a certain period of time, it is not available to others. The client wants the customer to be able to change the dates of the their order. This is fine. The order creates a reservation node and with permissions and hook_form_alter, I set it up so that they can edit the reservation dates.

However, I don't want them to be able to change the dates to whatever they want without first checking to see if it is already reserved. I have already written the code to run a query and return a boolean based on whether or not there is a booking conflict. Now I just need to be able to use that on the form submission handler.
You should do this in validation handler. In hook_form_alter (or in your form definition function), add $form['#validation'][] = 'mymodule_form_validate';. Then, add this code:

function mymodule_form_validate($form, &amp;$form_state) {
  if (!mymodule_dates_valid($form_state)) { // Return boolean here.
    form_set_error('date_field', t('Invalid dates'));
  }
}