Prevent form submission doesn't work when character limit exceeded

Created on 14 August 2020, over 4 years ago
Updated 9 October 2024, 2 months ago

Problem/Motivation

When I have enabled checkbox "Prevent form submission when character limit exceeded" for form element
I can submit form.

Expected behaviour: JS handler should prevent form from submitting

Steps to reproduce

1. Go to simplytest.me and create instance with textfield_counter module
2. Add restriction for Title field on Manage form display for Basic page
3. Create basic page node and type string with more then allowed amount of characters.
4. Submit form.

Form will be submitted and you will get backed validation message.

Expected behaviour: JS handler should prevent form from submitting

Proposed resolution

The issue is that JS handler trying to match field data-field-definition-id attribute vs key in textfieldCounter array in JS Drupal settings.
But they don't match by definition. See more here https://git.drupalcode.org/project/textfield_counter/-/blob/8.x-1.x/src/...

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇺🇦Ukraine andriyun

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇫🇷France mably

    I can confirm that this still doesn't work as we are comparing two different values :

    In the PHP code we are setting this in the fieldFormElement of the TextFieldCounterWidgetTrait:

        $element['#attributes']['data-field-definition-id'] = $field_definition_id;
        $element['#attached']['drupalSettings']['textfieldCounter'][$key]['key'][$delta] = $key;
    

    With $key and $field_definition_id having potentially two different values.

    And then we are comparing them back in textfield_counter.js :

    $.each(settings.textfieldCounter, function (settingsIndex, fieldSettings) {
                var wrapperElement = $(errorElements[elementIndex]);
                if (fieldSettings.preventSubmit && settingsIndex === wrapperElement.find(".textfield-counter-element:first").data("field-definition-id")) {
                  e.preventDefault();
    

    And it some cases, when used in a paragraph for example, they don't match and the submit is not blocked.

  • 🇮🇳India divyansh.gupta Jaipur

    Hello @andriyun,
    But I cannot reproduce the form submission error as it is working fine for me and showing me a error and my form is not submitting if i exceed the count limit.

  • 🇫🇷France mably

    @divyansh.gupta try adding a multi-valued reference field to a node pointing to a paragraph containing a text field with a counter.

    Then create a new instance of that content and add a paragraph with the text field exceeding the authorized length.

    You will see that you can submit the form without triggering the javascript preventDefault.

Production build 0.71.5 2024