0

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.

1 Answer 1

4

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, &$form_state) {
  if (!mymodule_dates_valid($form_state)) { // Return boolean here.
    form_set_error('date_field', t('Invalid dates'));
  }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.