White man holding scissors as if about to cut a laptop lid.

Trim strips leading and trailing whitespace from all text fields in content form submissions.

This works for content titles, plain text fields, and usernames, for example.

The module trims prior to form validation, so for instance if you have an integer field and a person entering data fat-fingers a space after a number, he gets a successfully saved node rather than an error message about invalid input. (Note: HTML validation triggers an error for blank spaces in input fields of type email or number before this module can act.)

Because blank spaces are likely trimmed or invisible in rendered HTML, you are most likely to see the corrective effects of this module in alphabetical lists such as from Views. So if the proprietor of We Didn't Think Alphabetical Order Mattered On The Internet tries to jump ahead of Aardvark Agency in your directory, she'll be disappointed.

Note that Drupal already trims whitespace from passwords without warning. Drupal's "Autocomplete (Tags style)" form field widget also trims all surrounding spaces from terms. Trim extends this behavior to all text fields on all content entities. (In the Drupal context, users and taxonomy terms are 'content'.)

Trimming all form entries everywhere was deemed too aggressive (for instance, if you have an options selection where you've made a key a single ' ' for some reason, or want to allow leading or trailing spaces on a field), so we do not trim input on configuration forms.

Trim module is based on exratione's blog post, Drupal: Trim All Form Fields Everywhere. Exratione deserves all credit if this module saves your sanity by sanitizing all stray spaces in your users' input. If it blows up your site, though, file a ticket here.

Note again that it trims values prior to saving, so it is in fact changing what people enter. This seems a reasonable exception to Drupal's rule of not messing with user input and doing all adjustments on output. If you think there should be an option to warn people when their input is trimmed, please contribute to #3388667: Alert people when their text is trimmed.

Caveats

Image credit: WikiMedia Commons

And again, credit to Exratione.

Supporting organizations: 
Needed the module and found exratione's blog post.
Helps maintain for Drutopia sites.

Project information

Releases