0

Ok not entirely sure if this is a bug with Drupals Form API or not, so thought I would ask here before opening an issue.

I have built a custom form using Drupals Form API and it all resides within a module.

I have pasted in the code after the explanation for clarity.

Issue I am having: When I use the code shown in the module file to apply a Twig template to the form, the submitForm and validateForm handlers within the Form class do not execute (Used xDebug in PHPStorm to verify).

If I comment out the code in the module file so that the template is not applied, the handlers begin working.

Note: With the module file code uncommented, the twig file (Located in root of module folder, called auction-enquiry-form.html.twig) is applied to the form correctly, but as previously stated the handlers don't work.

Am I missing something fundamental here ?

Drupal version: 8.6.1 Webserver: nginx/1.15.1 PHP Version: 7.2.4

Form Class (AuctionEnquiryForm.php) has the following items within it:

<?php

declare(strict_types=1);

namespace Drupal\lrg_auction_enquiry\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

class AuctionEnquiryForm extends FormBase
{
    public function getFormId() {
        return 'auction_enquiry_form';
    }

    public function buildForm(array $form, FormStateInterface $form_state){

        $form['frm_fullname'] = [
            '#type' => 'textfield',
            '#title' => $this->t('Fullname'),
            '#required' => true,
            '#attributes' => [
                'placeholder' => 'Full name *'
            ],
            '#title_display' => 'none'
        ];

        $form['frm_email_address'] = [
            '#type' => 'textfield',
            '#title' => $this->t('Email address'),
            '#maxlength' => 255,
            '#required' => true,
            '#default_value' => '',
            '#attributes' => [
                'placeholder' => 'Email address *'
            ],
            '#title_display' => 'invisible'
        ];

        $form['frm_your_question'] = array(
            '#title' => t('Your question'),
            '#type' => 'textarea',
            '#description' => 'Your question',
            '#default_value' => '',
            '#rows' => 6,
            '#cols' => 60,
            '#required' => true,
            '#resizable' => true,
            '#attributes' => [
                'placeholder' => 'Your question *'
            ],
            '#title_display' => 'invisible'
        );

        $form['#cache']['max-age'] = 0;

        $form['actions']['#type'] = 'actions';
        $form['actions']['submit'] = array(
            '#type' => 'submit',
            '#value' => $this->t('Submit'),
            '#button_type' => 'primary',
        );

        return $form;
    }

    /**
     * {@inheritDoc}
     */
    public function validateForm(array &$form, FormStateInterface $form_state){
        $emailAddress = $form_state->getValues()['frm_email_address'];

        if ($emailAddress !== '' && !\Drupal::service('email.validator')->isValid($emailAddress)) {
            $form_state->setErrorByName('Email Address', $this->t('Invalid email address format'));
        }
    }

    /**
     * {@inheritDoc}
     */
    public function submitForm(array &$form, FormStateInterface $form_state) {
        // todo: Change redirect to go to a specific thank you page. The thank you for this form is generic.
    }
}

Routing file (lrg_auction_enquiry.routing.yml) is as follows:

lrg_auction_enquiry.page:
  path: '/auctionsform'
  defaults:
    _form: '\Drupal\lrg_auction_enquiry\Form\AuctionEnquiryForm'
    _title: 'Auction Enquiry Form'
  requirements:
    _access: 'TRUE'

Module file (lrg_auction_enquiry.module)

<?php

function lrg_auction_enquiry_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
    if($form_id == 'auction_enquiry_form') {
        $form['#theme'] = 'auction_enquiry_form';
    }
}

function lrg_auction_enquiry_theme($existing, $type, $theme, $path) {
    $info = [
        'auction_enquiry_form' => [
            'render element' => 'form',
            'path' => drupal_get_path('module', 'lrg_auction_enquiry'),
        ],
    ];
    return $info;
}

Twig template (auction-enquiry-form.html.twig)

<h2>Online enquiry form</h2>
<h3>One of our friendly auctioneers will get back to you ASAP.</h3>
{{ form.frm_fullname }}
{{ form.frm_email_address }}
{{  form.frm_your_question }}
<div style="display:block;font-size:12px;width:100%;"><span style="font-size:14px;">*</span> Mandatory fields</div>
{{ form.actions.submit }}
  • Could you post the contents of auction-enquiry-form.html.twig? – Clive Oct 3 '18 at 14:43
  • Added twig content – John Cogan Oct 3 '18 at 14:50
  • 1
    You're probably missing the hidden elements (e.g. form_build_id). Those need to be rendered as well – Clive Oct 3 '18 at 15:05
  • Thanks Clive, thats perfect. After adding the build_id and form_id its all running smoothly now. Thanks, didn't even consider that. If you add as an answer I can award it to you – John Cogan Oct 3 '18 at 15:15

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.