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
Comment | File | Size | Author |
---|---|---|---|
#10 | Screenshot 2023-02-02 at 16.58.06.png | 46.08 KB | Schoenef |
#8 | Screenshot 2023-02-02 at 11.33.03.png | 81.07 KB | Schoenef |
#7 | webform.webform.kontakt.yml | 7.7 KB | Schoenef |
#4 | webform.webform.contact.yml | 7.69 KB | jrockowitz |
Comments
Comment #2
Schoenef CreditAttribution: Schoenef for flowconcept solutions commentedComment #3
Schoenef CreditAttribution: Schoenef for flowconcept solutions commentedUrks, 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.
Comment #4
jrockowitz CreditAttribution: jrockowitz as a volunteer and at Webform module Open Collective, The Big Blue House commentedUsing 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
Comment #5
Schoenef CreditAttribution: Schoenef for flowconcept solutions commentedDear @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!
Comment #6
jrockowitz CreditAttribution: jrockowitz as a volunteer and at Webform module Open Collective, The Big Blue House commented@see https://www.youtube.com/watch?v=zmRxyUHWczw
@see https://www.youtube.com/watch?v=tgF5bJt9vnY
Comment #7
Schoenef CreditAttribution: Schoenef for flowconcept solutions commentedDear @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.
Comment #8
Schoenef CreditAttribution: Schoenef for flowconcept solutions commentedOk, 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.
Am I missing something else?
Comment #9
jrockowitz CreditAttribution: jrockowitz as a volunteer and at Webform module Open Collective, The Big Blue House commentedCould 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?
Comment #10
Schoenef CreditAttribution: Schoenef for flowconcept solutions commentedI'm using a custom theme indeed - I just switched to Olivero, but the behaviour is also the same:
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).
Comment #11
jrockowitz CreditAttribution: jrockowitz as a volunteer and at Webform module Open Collective, The Big Blue House commentedMaybe try updating to Webform 6.2.x?
Comment #12
jrockowitz CreditAttribution: jrockowitz as a volunteer and at Webform module Open Collective, The Big Blue House commentedBTW, you should inspect the FORM and make sure there is no "novalidate' attribute on the FORM tag.
Comment #13
jrockowitz CreditAttribution: jrockowitz as a volunteer and at Webform module Open Collective, The Big Blue House commentedComment #14
akmalfikri CreditAttribution: akmalfikri commentedI 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
Comment #15
Liam MorlandDoes this happen on the latest development version of 6.2.x?
Comment #16
r_cheh CreditAttribution: r_cheh commentedYes, I have the same issue, form has attribute novalidate="novalidate", and inline validation don't work
Comment #17
jrockowitz CreditAttribution: jrockowitz as a volunteer and at Webform module Open Collective, The Big Blue House commentedI can't replicate this issue. If a theme or patch adds the
novalidate
attribute to theform
tag, clientside validation will not work as expected.