Problem/Motivation

I have a contact webform, wich opens in side a modal and submits via ajax. I'm using for this the core ajax functionality introduced in https://www.drupal.org/project/webform/issues/2757491. (I also tried to find a solution via SO, but it went pretty sideways: https://drupal.stackexchange.com/questions/314525/webform-ajax-how-to-al...)

I'm including the webform like this:

<div class="modal fade" id="contactModal" tabindex="-1" aria-labelledby="contactModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Schließen"></button>
      </div>
      <div class="modal-body pt-1">
        {{ { '#type': 'webform', '#webform': 'contact' } }}
      </div>
    </div>
  </div>
</div>

The webform has this config:

// ...
settings:
  ajax: true
  ajax_scroll_top: form
  ajax_progress_type: fullscreen
  ajax_effect: fade
  ajax_speed: null
// ...

Issue: If I submit it in the overlay, it loads via ajax, but the required fields are not validated clientside. I'm able to submit the form with empty fields, which leads then to a pretty massive error response and a generally unnessesary clumsy user experience.

Steps to reproduce

Create a webform with ajax enabled and some required field

Include the form in some template

Submit the form without filling any field - it submits and returns a lot of unnessessary error messages

Proposed resolution

Use at least the html5 client side validation, better the full webform client side validation js. Looking at the code, this should probably happen either in the ajax.options.beforeSubmit or ajax.options.beforeSerialize functions of the ajax api - see web/core/misc/ajax.js

Remaining tasks

User interface changes

API changes

Data model changes

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Schoenef created an issue. See original summary.

Schoenef’s picture

Issue summary: View changes
Schoenef’s picture

Urks, this appears to be really deeply in the core already. It is aparently possible to either override the Drupal.Ajax.prototype.beforeSend (https://www.drupal.org/forum/support/module-development-and-code-questio...) or potentially to do it for a specific call only: https://drupal.stackexchange.com/questions/1890/how-to-add-some-javascri...

This should not be done generally but only for webform ajax.

jrockowitz’s picture

FileSize
7.69 KB

Using the built-in modal support via the below code and see attached. Modals with clientside validation are working as expected.

<a href="/form/contact" class="webform-dialog webform-dialog-normal button">Test Normal</a>

There are some issues with the latest version of the clientside validation module.

@see #3110890: Cientside validation errors revert to being output as a label in webform wizard pages with ajax enabled
@see #3322946: Once is not well implemented

What version of the clientside validation module are you using? 3.x or 4.x

Schoenef’s picture

Dear @jrockowitz,

it looks like my webform knowledge has a lot of holes... (It's the first time I hear of the
Webform Clientside Validation module or build in modal support).

Incidentally it is not even activated in my project, so I'll try it out tomorrow and if it works. This whole ticket might be not an issue at all...

Thx for answering never the less!

jrockowitz’s picture

Schoenef’s picture

Dear @jrockowitz,

so I used your example yml form and enabled the webform client side validation module. It does work for your example (which has not ajax enabled) on the webform page, but I was not able to get it as model using

<a href="/form/kontakt" class="webform-dialog webform-dialog-normal button">Test Normal</a>

Any other module I'm missing?

However - if I enable ajax, the client side validation is ignored again (please see the updated yaml of your form - I had to rename it to kontakt thou)

So I think it really is a bug, ajax should not prevent the client side validation in my opinion. Ideally also it would not be necessary to install any extra plugin for the basic html5 client side form validation api to kick in.

Schoenef’s picture

Ok, I just found the "Enable Side Wide Modal Support" checkbox - thx a lot for the helpful videos.

however, this is exactly the same behaviour (ajax dissabled this time) - the form in the modal is turned automatically into some ajax thing - if I submit it empty, the validation is happening only server side, not client side.

After Modal submit

Am I missing something else?

jrockowitz’s picture

Could you let me know if you are using a custom theme? What happens with Bartik or Olivero?

Is anything being logged in the browser's JS console?

Did you enable the webform_clientside_validation.module?

Schoenef’s picture

I'm using a custom theme indeed - I just switched to Olivero, but the behaviour is also the same:

Olivero Errors

I have Webform Clientside Validation, Clientside Validation jQuery and Clientside Validation modules enabled. The console shows no errors, it only logs the Ajax POST request on submit (Brave Browser).

jrockowitz’s picture

Maybe try updating to Webform 6.2.x?

jrockowitz’s picture

BTW, you should inspect the FORM and make sure there is no "novalidate' attribute on the FORM tag.

jrockowitz’s picture

Status: Active » Closed (cannot reproduce)
akmalfikri’s picture

I can reproduce this issue.

Steps :

Requirements
1. Client side validation module & jquery
2. Webform client side validation submodule

1. Create a webform
2. Add the webform link via dialog in any page
3. Click the button
4. Click submit

Errors will be displayed in messages, not inline.

For reference : https://youtu.be/KClbJAXly7I

Liam Morland’s picture

Version: 6.1.4 » 6.2.x-dev
Status: Closed (cannot reproduce) » Active

Does this happen on the latest development version of 6.2.x?

r_cheh’s picture

Yes, I have the same issue, form has attribute novalidate="novalidate", and inline validation don't work

jrockowitz’s picture

Priority: Major » Normal
Status: Active » Closed (cannot reproduce)

I can't replicate this issue. If a theme or patch adds the novalidate attribute to the form tag, clientside validation will not work as expected.