Replace $form['array_filter'] hack with #element_validate

Dave Reid - September 30, 2008 - 07:12
Project:Drupal
Version:7.x-dev
Component:system.module
Category:bug report
Priority:normal
Assigned:Dave Reid
Status:needs work
Description

I noticed that when I am using

<?php
$form
['array_filter'] = array('#type' => 'value', '#value' => TRUE);
?>

in my forms with checkboxes, that there is a variable 'array_filter' being saved into my variables table.

I looked back at some of the history of array_filter issues, and noticed that #41170: Don't save junk as variables during settings save was meant to fix this, but a different fix was committed that does not prevent the saving of array_filter. The attached patch modifies system_settings_form to not save the 'array_filter' form option as a variable.

AttachmentSizeStatusTest resultOperations
array_filter-D7.patch1.46 KBIgnoredNoneNone

#1

Dave Reid - September 30, 2008 - 07:14
Title:system_settings_form saves undesired 'array_filter' variable» system_settings_form_submit saves undesired 'array_filter' variable

This bug is also present in D6 and D5 and should be backported if this is deemed an acceptable solution.

#2

Dave Reid - September 30, 2008 - 07:24

Here are the D5 and D6 patches for this issue just for kicks. Don't hurt me testing bot...

AttachmentSizeStatusTest resultOperations
array_filter-D6.patch1.48 KBIgnoredNoneNone
array_filter-D5.patch1.26 KBIgnoredNoneNone

#3

Dave Reid - September 30, 2008 - 08:03

Here is another proposed solution, that removes the FAPI hack 'array_filter' and adds a new function drupal_filter_array() in form.inc that can be used with #element_validate:

<?php
$form
['item']['#element_validate'] = array('drupal_filter_array');
?>

AttachmentSizeStatusTest resultOperations
array_filter-2-D7.patch2.64 KBIgnoredNoneNone

#4

Damien Tournoud - September 30, 2008 - 08:25
Status:needs review» needs work

I love #3, that's so Form API compliant :)

We should include an update function that remove array_filter from the variable table.

#5

Dave Reid - September 30, 2008 - 17:01
Status:needs work» needs review

New patch:
- Adds a function drupal_filter_form_array that replaces the FAPI hack $form['array_filter'] with $form['item']['#element_validate'] = array('drupal_filter_form_array');
- Adds system_update_7011 to delete the unwanted 'array_filter' variable that was accidentally saved.
- Picked up a few coding space standard issues.

AttachmentSizeStatusTest resultOperations
array_filter-3-D7.patch4.48 KBIgnoredNoneNone

#6

Dave Reid - October 1, 2008 - 05:43
Title:system_settings_form_submit saves undesired 'array_filter' variable» Remove $form['array_filter'] hack with #element_validate

Renaming for new direction and bump for review or thoughts. Anyone?

#7

Dave Reid - October 1, 2008 - 05:44
Title:Remove $form['array_filter'] hack with #element_validate» Replace $form['array_filter'] hack with #element_validate

#8

dropcube - October 3, 2008 - 01:47
Status:needs review» needs work

According to the recommendations of chx in IRC, it's better to use #process. Any #process handlers attached to a specific element are executed before they are processed.

#9

Dave Reid - November 19, 2008 - 01:30

I need some help with this...I have no idea how to get #process working to filter array values on submission... Where's my patch status of 'code needs help'? :)

#10

Dave Reid - January 6, 2009 - 21:40
 
 

Drupal is a registered trademark of Dries Buytaert.