Allow users to type past the character limit on hard limits and display an error upon save.

Created on 4 March 2022, almost 3 years ago
Updated 17 January 2023, almost 2 years ago

Problem/Motivation

When a field has a hard character limit, most authors prefer to still be able to type past the character limit. This way they know how far they are exceeding the limit by and can adjust accordingly.

Proposed resolution

Instead of preventing a user from typing past the character limit, allow them to do so but display an error when the user tries saving the form.

An error message should display at the top of the page stating, " [Field Name] cannot be longer than [maxlength] characters but is currently [current] characters long."

The hard limit configuration help text should read, "If enabled, the author is prevented from saving the form if the character limit exceeds the maxlength."

Remaining tasks

  1. Remove checkbox Enforce limit on saving form
  2. Incorporate error upon save behavior to Hard limit option.
  3. Apply this hard limit behavior to all Maxlength compatible fields (title, formatted text, plain text, link)
  4. Update Hard limit option help text to state, "If enabled, the author is prevented from saving the form if the character limit exceeds the maxlength."
  5. Fix character count not updating.
  6. Test

User interface changes

Yes, to the text field and the Maxlength configuration settings.

API changes

None

Data model changes

None

✨ Feature request
Status

Needs work

Version

2.0

Component

User interface

Created by

πŸ‡ΊπŸ‡ΈUnited States cedewey Denver, CO

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.

  • Status changed to Postponed almost 2 years ago
  • πŸ‡ΊπŸ‡ΈUnited States cedewey Denver, CO
  • πŸ‡«πŸ‡·France prudloff Lille

    Instead of implementing our own validation function, we might be able to simply add #maxlength to the field and let FormValidator::performRequiredValidation() validate it: https://www.drupal.org/node/2887280 β†’

  • πŸ‡ͺπŸ‡ΈSpain albertosilva Basque Country

    This patch adds #maxlength to the field, sot FormValidator::performRequiredValidation() validates it, as proposed in #17.

  • πŸ‡΅πŸ‡±Poland gugalamaciek

    #18 works, but it still won't allow to paste strings longer that #maxlength.

    On module configuration, I'd like to have these configuration options to define how limit should be treated:

    1. Allow input and saving of text that exceeds the specified limit. This option is available in current version of the module. It just displays information that you exceeded limit on edit form, but nothing more.
    2. Block input and saving of text that exceeds the set limit. This option is provided by patch #18. It's partially supported by hard limit option in current version of the module - the missing thing is to add validation during save. But #18 solves it.
    3. Allow adding text beyond the specified limit, but show an error message when attempting to save. This is the missing puzzle and most tricky one.

    I analysed code of the module. I think that before we'll be able to implement last option, we have to refactor part of the module. The case it that MaxLength settings are defined on widget level. But, to implement last option, we have to add constraint on field level (to validate during save). So, all the configuration should be moved from widget level to field level. As example, see implementation of unique_field_ajax β†’ module. It's very similar, but instead of validating length, it validates uniqueness.

    When I think more, it's also one more good reason to move configuration from widget to field level. If you configure things on that level, no matter which widget you select, they will be applied. You don't need to check widget type, it's enough to define field types that should be able use MaxLength feature. But what if I define my custom type in my module and I want to add MaxLength to it? So this list should be alterable. Or... in admin/config create section for MaxLength. And every webmaster will be able to decide for which field types module should be enabled. Also we can use plugin system and define something like MaxLength processors. That way any user can extend this module to it's custom field with custom processor to define how this should be validated.

    These are like ideas, how it can be implemented from my head, without deeper analysis. Also I don't have knowledge that inspired authors of the module to write it in a way that's written now. Some concepts might be to havy, some missing. But it sounds for me more like version 4.x of the module.

Production build 0.71.5 2024