Download & Extend

custom filtering of form field values

Project:Drupal core
Version:4.7.x-dev
Component:base system
Category:feature request
Priority:normal
Assigned:Unassigned
Status:closed (fixed)

Issue Summary

The attached patch adds a #filter property to fields which allows cleanup of user values post-validation, given there are no errors. As an example, I find PHP's built-in trim() function to be a particularly useful filter (try '#filter' => array('trim' => array())), and stripping leading or trailing slashes from paths is always useful...

AttachmentSizeStatusTest resultOperations
form.inc.filter.patch2.34 KBIgnored: Check issue status.NoneNone

Comments

#1

Re-rolled patch for 1.28.

AttachmentSizeStatusTest resultOperations
form.inc.filter_0.patch2.37 KBIgnored: Check issue status.NoneNone

#2

is this step needed? requires changes to docs as well. perhaps use the validate step for filtering. also, the word 'filter' is used already. this is a 'post validate', 'pre save' step.

#3

Alex, why #submit is not proper for this? That's a post validate step and can be made so that it's before the $form_id .'_submit'

#4

I like the #filter property for the following reasons:

  • filters are applied only if validation finishes without errors (same as #submit)
  • filters are applied to individual fields, and often don't need to be custom functions (as in the case of trim())
  • to me, it makes more sense to define the task of filtering a field within that field's array, rather than in the global form's #submit routine which then must find the particular field to be filtered (this is harder to read, requires more form-specific code, and results in a less complete encapsulation of a field)

While it's true that the #filter property requires more code in form.inc, it will reduce the amount of form-defining code in modules, if it's used enough. I use it all the time with Formproc, but perhaps that's unusual.

#5

Alex, #submit can be any number of callbacks so I still think that you can get away with it. You can define #submit array in the form element defining array.

#6

Version:x.y.z» 4.7.x-dev
Status:needs review» closed (fixed)

-1. I agree with chx: just use #submit for filtering.

Closing issue due to a 1-year period of inactivity.

nobody click here