0

I have a multistep checkout form in my commerce module. Inside Order Types i have added a field called Phone Number and set it to required. And then I have a custom module which has a ContactInformation.php file under CheckoutPane which adds the field in buildPaneForm

    /**
 * Provides the contact information pane.
 *
 * @CommerceCheckoutPane(
 *   id = "contact_information",
 *   label = @Translation("<h2 tabindex='0'>Contact information</h2>"),
 *   default_step = "order_information",
 *   wrapper_element = "fieldset",
 * )
 */
class ContactInformation extends \Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\ContactInformation implements CheckoutPaneInterface {
  /**
   * {@inheritdoc}
   */
  public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
    $pane_form = parent::buildPaneForm($pane_form, $form_state, $complete_form);
    $form_builder = \Drupal::service('entity.form_builder');
    $order_form = $form_builder->getForm($this->order, 'default');
    $user_entry = $form_state->getUserInput();

    $pane_form['field_phone_number'] = $order_form['field_phone_number'];
    $pane_form['field_phone_number']['#weight'] = 99;

    if (isset($user_entry['field_phone_number'])) {
      $pane_form['field_phone_number']['widget'][0]['value']['#value'] = $user_entry['field_phone_number'][0]['value'];
    }

    return $pane_form;
  }

In my checkout form I am disabling HTML5 validation by setting $form['#attributes']['novalidate'] = 'novalidate';

Upon submitting the form without entering any data, the server side catches all the fields that are required but empty with a message saying the field is required (Email, First Name, Last Name, etc...). All the fields except my Phone Number field.

In debug mode I see that the field has a key ['#required'] = true but the server side validation is not catching this field.

Any help would be appreciated. Let me know if there's anything else you need that might help solve this issue.

  • I don't see any required attributes in your code. – Jaypan Dec 10 '19 at 20:26
  • When i created the field in the UI i selected Required. I also checked in xDebug and required is set to true. Also, I did try putting $pane_form['field_phone_number']['#required'] = true; in there as well to see if that would somehow solve the problem, but it did not. – Rudi Shafakian Dec 10 '19 at 20:32
  • You are setting the #value on the phone number. Change that to #default_value and see what happens. – Jaypan Dec 10 '19 at 20:35
  • I tried this and still the same outcome. – Rudi Shafakian Dec 10 '19 at 20:40
  • I am only setting the value if it exists. If I remove that part completely then it is still the same. – Rudi Shafakian Dec 10 '19 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.