I am trying to collect additional information on checkout to add to the order. I have added new fields to the order entity and then used hook_form_commerce_checkout_flow_multistep_default_alter to add "fake" fields to the order_information form. Then, using hook_entity_presave()
, I take the values from the fake fields and set the real order fields. This all works as expected.
As some of these fake fields are conditionally required to be entered, I am trying to add a validate function. In the form alter I have added $form['actions']['next']['#submit'][] = '_altpay_form_validate';
and for that function I have:
function _altpay_form_validate(&$form, $form_state) {
$who_paying = $form_state->getValue('altpay');
if (!$who_paying) {
$form_state->setErrorByName('altpay', t('You must select who is paying for this order.'));
}
}
Calling setErrorByName()
crashes with the following exception.
LogicException: Form errors cannot be set after form validation has finished.
I tried setting Validation Complete to FALSE
and then setErrorByName()
no longer crashes, but also does not prevent form submission.
I suspect this has something to do with the multi-step nature of this form.
Do you have any hint on what I am missing?