Settings from fields not validating correctly.

Created on 27 April 2023, over 1 year ago
Updated 29 May 2024, 7 months ago

Problem/Motivation

Trim length should only accept numeric values.

More Link Text should be a required field (if More link checkbox checked).

Should the aria-label and class fields be required when More link is checked?

Steps to reproduce

In Smart Trim settings form, enter non-numeric characters in Trim length. Field will be saved without complaint. Obviously this will not work.

In More field, delete all text. Field will save.

Proposed resolution

The ideal solution would be to add a validation function to the settings form. Unfortunately a Field Formatter form is not a normal Drupal form, and does not provide a validation form. I've looked at adding a "#validation" array, but that didn't have any effect either (I suspect because the settings form isn't at the top level, but embedded in a larger form). There may be other ways to add a validation function, but I'm not sure what they are.

Trim length is currently a "textfield". I think changing it to a "number" would enforce browser validation, which should prevent non-numeric characters. However, this is dependent on browser support, and doesn't prevent spoofing the form (though presumably anyone with permission to tinker with formatter forms is a site admin and not motivated to break the site.

I've experimented with adding a "required" entry to the "#states" entry for the More text field. This adds the red "*" to the field, but does not enforce validation, which is disappointing.

Finally, a JavaScript validator would also be an option.

Remaining tasks

Add validation.

User interface changes

The UI should reject invalid input.

API changes

None.

Data model changes

None.

🐛 Bug report
Status

Needs work

Version

2.0

Component

Code

Created by

🇮🇪Ireland lostcarpark

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

Comments & Activities

  • Issue created by @lostcarpark
  • 🇺🇸United States ultimike Florida, USA

    All valid questions - especially the "trim units" field not being an integer 😱

    A couple of thoughts:

    1. Let's tackle this one when/if 📌 "More link" formatter configuration UI improvements Fixed is committed.
    2. We should take a look at other core/contrib modules to see how/if they handle validation in formatter config.

    -mike

  • 🇮🇳India sanket.addweb

    I resolved the issue by changing the field type from 'textfield' to 'number' and making the 'More text URL' field required when the 'Display More link?' checkbox is checked.

  • 🇮🇪Ireland lostcarpark

    @sanket.addweb, looks an interesting solution.

    Changing the field to a number makes sense.

    Making the "More text URL" required when "Display more link" is checked, but from a quick look, this is only implemented on the form generation. If the user checks "Display more link", the URL field will be shown, but will not be required until the form is submitted. The ideal would be to add a validate function to check if the field is populated, but it doesn't appear to be possible to add validation to a field formatter.

  • 🇮🇳India sarwan_verma

    I have fixed this issues and some change.

  • 🇮🇪Ireland lostcarpark

    One minor point, the checkboxStatus or testcheckboxStatus function in the above patches is only called from within the class, so I think it should be a protected member rather than a public one.

  • 🇮🇳India sanket.addweb

    @lostcarpark, I have made the necessary changes by replacing the public function with a protected function. Additionally, I conducted research on the "validateSettingsForm()" method and applied the validate handler in the formatter's setting form. However, the current implementation is not functioning as expected. I will continue investigating to identify and resolve the issue.

  • Status changed to Needs review 8 months ago
  • 🇮🇳India sanket.addweb

    @all, Moving to need review status

  • Status changed to RTBC 7 months ago
  • 🇮🇳India hasmimeraj

    Applied patch #7 to smart_trim:^2.1 and manually tested with drupal 10.2.6. All looks good.
    Since field settings are updated by ajax request. So error message are stored in session.
    And when you finally save the changes then error message appear with successfully settings saved message.

    Btw this is expected behavior. So marking this for RTBC

  • Status changed to Needs work 7 months ago
  • 🇺🇸United States ultimike Florida, USA

    @sanket.addweb - thanks for your efforts on this. Moving back to "Needs work" because we need to add at least one test for this. I would like to see a test that attempts to set the "Trim units" field to a non-integer and maybe also a test that covers the #required bit.

    -mike

Production build 0.71.5 2024