Required radios/checkboxes are not validated

riverfr0zen - October 1, 2007 - 11:23
Project:Drupal
Version:7.x-dev
Component:forms system
Category:bug report
Priority:critical
Assigned:Unassigned
Status:needs review
Issue tags:Annoying And Stupid Bugs That We Really Need To Just Fix Already
Description

It appears that radio/checkboxe fields that are set to be 'required' in the CCK type are not being evaluated for input within this module's context. To reproduce:

1) Create a nodeprofile type containing both a required text field, and a required radio field,
2) Integrate it with registration.
3) Register a user, without filling in the required fields in the nodeprofile.

If reproduced properly, you should receive a validation error saying the text field is required - but notice there is no such validation performed for the radio field. Despite the required field not being filled in, the nodeprofile is saved, and the user is registered.

4) Go to Administer > Content and try to edit and save the profile that was just created

(Note, this is now *outside* of the context of the nodeprofile module) If reproduced properly, you will find that here, in the regular content edit form returns a validation error (as expected) for the required radio field.

My suspicion is that the issue really lies in subform_element, but I haven't looked deeply into it yet.

#1

smooshy - October 2, 2007 - 20:36

I have this same problem. Has anyone come up with a solution?

#2

riverfr0zen - October 2, 2007 - 20:48

Until this gets fixed, my quick solution (and sucky) solution was to change radios/checkboxes to select fields.

#3

fago - October 12, 2007 - 14:06
Project:Node Profile» Subform Element
Version:5.x-1.2» 5.x-1.3

hm, I can reproduce the problem. It's a bug of the subform element module. Unfortunately, it's quite time intensive to go after this, and currently I've not much time for it - so I'll look at it when I find the time for it..

Perhaps, you could help me a bit with this and test if things change with other versions of the subform element module.

#4

qingdao - November 1, 2007 - 19:58

im having the same problem, will this bug be fixed soon ? (next some weeks)

#5

bjacob - December 19, 2007 - 17:52

Subscribing... Any updates yet?

#6

qingdao - January 10, 2008 - 11:02

to realize "required check" open the file "optionwidgets.module" (modules/cck) and replace line 117
'#required' => FALSE,
with
'#required' => $field['required'],

after this it will work

unfortunatelly the checkboxes are not marked with the red dot, which tells the user that this input is required, but at least the validation is working ...

#7

rlnorthcutt - January 29, 2008 - 21:13

I made the change to the "optionwidgets.module" above, and now the required radio button DOES show the red *... but it doesn't require the radio to be selected...

It seems like its not validating.

ron

#8

idmacdonald - February 1, 2008 - 14:26

I can confirm this bug. It seems to occur when using the integer field type and either of the checkbox widgets (single or multiple). It does seem to be a subform problem, because when editing the node normally, everything works as expected. However, when editing the node via nodeprofile (and subform element), this bug crops up. The value in the database is not changed when I uncheck a box that was previously checked. So, somehow '0' or null input values are not being passed correctly by the subform module.

If I set the 'off' value for the checkbox to a non-zero integer value, then the bug doesn't occur. So, it would seem to be a form of the PHP zero problem.

-Ian

#9

qingdao - February 2, 2008 - 13:18

this code changes affect only for the checkbox and not the radio box

#10

rleigh - February 11, 2008 - 07:36

OK, after some painful hours of tracing and examination, I figured out why this is happening.

This all happens in form.inc.

Right at the top of 'drupal_prepare_form' is the following code:

  if (!isset($form['#post'])) {
    $form['#post'] = $_POST;
    $form['#programmed'] = FALSE;
  }
  else {
    $form['#programmed'] = TRUE;
  }

The actual error code should be set in the '_form_validate' function, but it won't get set if there's no '#needs_validation' element.

The '#needs_validation' element should get set in the 'form_builder' function towards the bottom, but it won't happen because it's in a conditional statement that tests for $form['#programmed'], which is TRUE because subform set the post.

Now, HOW do we fix it? Easy to do with a patch to form.inc, but that's lame.

I was looking at adding a $form['#after_build'] function, since I don't see a hook that will work, but haven't gotten it to work yet.

Any ideas?

#11

rconstantine - February 21, 2008 - 01:21

subscribing

#12

ellen-dlma - March 3, 2008 - 14:10

subscribing

#13

tanjerine - March 20, 2008 - 18:11
Title:Radio/checkboxes set to 'required' are not validated» subscribing

subscribing

#14

bjacob - March 26, 2008 - 16:41
Title:subscribing» Radio/checkboxes set to 'required' are not validated

changed title ;)

#15

izmeez - April 13, 2008 - 05:33

I have noticed the same problem in the Drupal 6.x Profile module where I have one check box that is required. When the user first creates an account if they ignore this field they are not reminded that it needs to be completed. When the user later returns to edit their profile they do receive a reminder that the field must be completed. Is there a patch that would work for this? Thanks,

Izzy

#16

kenorb - April 15, 2008 - 13:57

The same problem.
subscribing
Tested on Drupal 5.7 + CCK 1.6

#17

kenorb - April 15, 2008 - 15:24
Status:active» needs review

Made a simple patch.
Problem was that #value wasn't empty() after nothing was selected.

AttachmentSizeStatusTest resultOperations
form.inc_3.patch463 bytesIdleFailed: Failed to apply patch.View details | Re-test

#18

izmeez - April 16, 2008 - 20:39

Is this patch also for Drupal v6.2 ?

Thanks,

Izzy

#19

noahb - April 17, 2008 - 10:24

subscribing

#20

xxparanormalxx - April 19, 2008 - 14:06

subscribing

#21

smooshy - April 30, 2008 - 21:06

the patch doesn't appear to work for my situation. When I edit it through the pageroute page, the value never gets set to 'off'

#22

TyraelTLK - May 16, 2008 - 14:24

Is the same problem that affects Profile core module in drupal 6.2?
http://drupal.org/node/259292

#23

Chill35 - May 25, 2008 - 03:36
Project:Subform Element» Drupal
Version:5.x-1.3» 6.x-dev
Component:Code» forms system

Is this a problem in form.inc ? The patch is for form.inc.

If so, this should not be associated with the module Subform Element anymore.

This problem is in Drupal 6.

#24

ejakila19 - May 23, 2008 - 03:35

I had this problem with radio buttons and I was surprised to find out that there is no validation function in optionwidgets.module for required fields.

My solution was to add the case for 'validate' in the optionwidgets_widget function:

function optionwidgets_widget($op, &$node, $field, &$items) {
.
.
.
     <new code>
     case 'validate' :
          _optionwidgets_widget_validate($node, $field, $items);
          break;
     </new code>
     ...
     case 'prepare form values':
          $options = _optionwidgets_options($field, $node);
.
.
.

then I added the following function to optionwidgets.module:

function _optionwidgets_widget_validate($node, $field, $items) {
     if (!$field['required']) {
          return;
     }

     switch ($field['widget']['type']) {
          case 'options_buttons' :
               if (!$items['key']) {
                    form_set_error($field['field_name'], t('@field is required.', array('@field' => $field['widget']['label'])));
               }
          break;
     }
}

This should fix the case for radio buttons, but it doesn't address checkboxes. To do so, you must add "case 'checkboxes' : ...code..." to the validation function just as I have done for 'options_buttons'

I hope this helps somebody

#25

Robert Castelo - May 25, 2008 - 00:29

I've created a generic solution here:

http://drupal.org/project/checkbox_validate

Just install the module and all checkboxes in every form display and behave the way they should.

I like this solution because it means developers don't need to add any extra validation code to their modules to make up for the bug in core, and once core is fixed this module can be uninstalled.

If your module relies on required checkbox fields be sure to make this module a dependency.

#26

webchick - May 25, 2008 - 03:11

See also #259292: unckecked checkbox set to "required" bypasses validation which I marked a duplicate of this bug.

#27

Chill35 - May 25, 2008 - 03:21

Wonderful, Robert, thank you :-)

#28

bjacob - June 11, 2008 - 06:59

Hi Robert,

thanks for your patch. Can you provide a patch for Drupal 5.x as well?

@ejakila19: I've applied your solution. Here's the code for a single on/off checkbox:

function _optionwidgets_widget_validate($node, $field, $items) {
...

    case 'options_onoff':
      if (!$items['keys']) {
        form_set_error($field['field_name'], t('The field @field is required.', array('@field' => t($field['widget']['label']))));
      }
      break;
}

Thanks a lot, Bjoern

#29

Robert Castelo - June 15, 2008 - 14:24

Hi bjacob, I think the module should also work on Drupal 5, maybe .info file might need adjusting to install properly.

#30

deviantintegral - September 9, 2008 - 18:45

#17 doesn't work for me either. This even effects simple forms with a required checkbox for me.

--Andrew

#31

Dave Reid - September 9, 2008 - 21:06

Does anyone know if this been fixed in D7?

#32

Antinoo - September 19, 2008 - 11:38

-12 days, and this bug will be 1 year old :-)

:-(

#33

webchick - September 19, 2008 - 18:27

Then someone better get on reviewing the patch and providing results, and then marking it RTBC. :P

#34

Dave Reid - October 22, 2008 - 16:57

Marked #224012: required fields don't work correctly as a duplicate of this issue.

#35

misiu9 - January 4, 2009 - 22:06
Version:6.x-dev» 6.3
Priority:normal» critical
Status:needs review» active

Is this problem fixed in any recent version of drupal???

Im using drupal 6.3 and the problem still exists with radio buttons and check boxes which are marked as required.

During registration the required check boxes are being ignored.

Thanks for any help

Michael

#36

Dave Reid - January 5, 2009 - 20:45
Version:6.3» 7.x-dev
Priority:critical» normal

No, this has not been fixed yet and for all I know, is still present in 7.x where it will need to be fixed first, and then backported to 6.x and 5.x. This is also not a critical patch because it does not need to be fixed immediately because the software is not usable at all.

#37

sun - January 6, 2009 - 04:21
Title:Radio/checkboxes set to 'required' are not validated» Required radios/checkboxes are not validated
Version:7.x-dev» 6.x-dev

I am not able to replicate this bug in 7.x. Setting #required => TRUE on radios and checkboxes properly returns a validation error.

#38

Dave Reid - January 6, 2009 - 04:41

Thanks for checking sun!

#39

deviantintegral - January 6, 2009 - 17:57
Version:6.x-dev» 7.x-dev

Actually, I can replicate this on HEAD. It only happens with the checkbox type - in fact, I've never had a problem with checkboxes / radios. The #259292: unckecked checkbox set to "required" bypasses validation is actually a better title for this issue.

Here is a pre-made module to easily test this. I will try and come up with a simpletest for this issue after lunch :)

AttachmentSizeStatusTest resultOperations
checkbox_test.tar_.gz756 bytesIgnoredNoneNone

#40

deviantintegral - January 6, 2009 - 19:12
Assigned to:Anonymous» deviantintegral

I have a patch for it which came about working on how to test this issue. I'll post both when I'm done the test.

#41

deviantintegral - January 6, 2009 - 20:11
Status:active» needs review

Here is the patch along with the test. The patch does two things;

  1. Modifies _form_validate() to notice the case where #type == 'checkbox', and if that's the case, don't allow #value to be zero. I believe this bug was introduced in #117748: Trim required fields on validate.
  2. Modifies theme_checkbox() so that required checkboxes show the required state. Currently, #title is unset in theme_checkbox, which means that the required state is never added (as there's no label element for single checkboxes). As far as I can tell from the CVS logs, this function never properly themed required checkboxes. As anyone overriding this theme function may have started with this code, I'm going to suggest that a special note of this be made in the release notes for themers.

This passes all tests currently.

AttachmentSizeStatusTest resultOperations
179932_required_checkbox_validate.patch4.03 KBIdleFailed: Failed to install HEAD.View details | Re-test

#42

Gábor Hojtsy - January 7, 2009 - 10:43

#43

Dries - January 7, 2009 - 12:17

The code looks good to me, although that check in form.inc is getting a little nasty.

The change to theme_checkbox I'm not 100% sure about.

Thanks for adding a test!

I'll let some more people review this patch.

#44

System Message - January 8, 2009 - 01:05
Status:needs review» needs work

The last submitted patch failed testing.

#45

deviantintegral - January 18, 2009 - 00:40
Status:needs work» needs review

I was just able to install HEAD with this patch applied, so flipping back to CNR.

#46

Damien Tournoud - January 18, 2009 - 02:14
Status:needs review» needs work

Please leave this poor check alone, it totally makes sense. The issue is in form_type_checkbox_value()... but there could be more than one bug there.

#47

Damien Tournoud - January 18, 2009 - 02:44
Assigned to:deviantintegral» Anonymous
Status:needs work» needs review

form_type_checkbox_value() had a bug, that #229129: System module page *seriously* broken tried to fix totally the wrong way.

The bug is that

<?php
return !empty($edit) ? $form['#return_value'] : 0;
?>

should be:

<?php
return !empty($edit) ? $form['#return_value'] : NULL;
?>

Because a non-checked checkbox should not return any value, not the '0' value. This breaks both the logic that is triggered later on to "set the default value if no value was found" (which #229129: System module page *seriously* broken tried to fix) and the "a required checkbox is not validated because even a non-checked checkbox has a value of '0'" (which this patch tried to fix the wrong way).

The attached patch:
- makes the correct fix,
- reverts the silly unit tests that have been committed in #314283: TestingParty08: Disabled checkboxes need tests, in an effort to make sure that the crappy fix was working,
- adds a new test,
- add the fix to theme_checkbox, that IMHO makes sense (there is a big of redundant code with theme_form_item(), but we can't really do anything else because we need to output the checkbox *inside* the label tag)

AttachmentSizeStatusTest resultOperations
179932-required-checkbox-validate.patch5.83 KBIdleFailed: 8710 passes, 308 fails, 20 exceptionsView details | Re-test

#48

System Message - January 18, 2009 - 03:35
Status:needs review» needs work

The last submitted patch failed testing.

#49

webchick - January 18, 2009 - 04:31

I can confirm the results of the testing bot. This still needs work.

#50

Damien Tournoud - January 19, 2009 - 11:51
Status:needs work» needs review

Now that I think about it at a non-crazy hour, we really do need that special #disabled handling because there is no way to differentiate between a "non-checked checkbox" and a "disabled checkbox".

So I rewrote and documented form_type_checkbox_value this way:

<?php
   
if (isset($edit)) {
     
// A value is passed by the browser: the checkbox is on.
     
return $form['#return_value'];
    }
    else {
      if (!empty(
$form['#disabled'])) {
       
// Disabled checkbox values are not passed by the browser,
        // use default instead.
       
return $form['#default_value'];
      }
      else {
       
// The checkbox is off.
       
return '';
      }
    }
?>

I also added a unit test of checkboxes, which could be extended at a later stage to test other widget types.

100% on my development environment, let's see what the bot thinks.

AttachmentSizeStatusTest resultOperations
179932-required-checkbox-validate.patch11.6 KBIdleFailed: Failed to apply patch.View details | Re-test

#51

System Message - January 19, 2009 - 12:05
Status:needs review» needs work

The last submitted patch failed testing.

#52

Damien Tournoud - January 19, 2009 - 12:12
Status:needs work» needs review

Reroll.

AttachmentSizeStatusTest resultOperations
179932-required-checkbox-validate.patch11.51 KBIdleFailed: 8957 passes, 4 fails, 2 exceptionsView details | Re-test

#53

System Message - January 19, 2009 - 13:15
Status:needs review» needs work

The last submitted patch failed testing.

#54

Damien Tournoud - January 19, 2009 - 13:40

I can't reproduce the testing failures.

#55

geerlingguy - February 3, 2009 - 05:21

D'oh! Just ran into this problem in Drupal 6. Kind of annoying bug, as I'd like to have an "I read the Privacy Policy blah blah" checkbox on the user registration form, but without this validation, it doesn't have to be checked...

#56

kenorb - February 4, 2009 - 11:16

#55 -> #25

#57

geerlingguy - February 6, 2009 - 04:14

@ kenorb - ah, thanks. I saw that, but my brain must've been turned to 'stupid' mode and I didn't regard it as useful to me. Installing now!

#58

noahb - February 6, 2009 - 05:07
Status:needs work» needs review

Re-roll of the patch from #52.

AttachmentSizeStatusTest resultOperations
checkbox.patch11.3 KBIdleFailed: 9664 passes, 7 fails, 2 exceptionsView details | Re-test

#59

System Message - February 6, 2009 - 05:35
Status:needs review» needs work

The last submitted patch failed testing.

#60

Jhef.Vicedo - March 24, 2009 - 08:11

subscribing

#61

Damien Tournoud - March 24, 2009 - 17:33
Status:needs work» needs review

Let's try again.

AttachmentSizeStatusTest resultOperations
179932-required-checkbox-validate.patch11.31 KBIdleFailed: 10553 passes, 14 fails, 5 exceptionsView details | Re-test

#62

System Message - March 24, 2009 - 17:45
Status:needs review» needs work

The last submitted patch failed testing.

#63

seanr - April 4, 2009 - 15:48
Priority:normal» critical

Why are we considering this to be a normal bug instead of critical? This effects every module that uses the checkbox field, and as geerlingguy noted, it represents a serious liability in certain cases.

In my particular case, it is actually causing a LEGALLY REQUIRED (as in federal election law!) checkbox not to validate properly. I now have to write extra code into my module that I would never have expected to need because Drupal core isn't performing the validation as advertised.

Let's please agree that this bug is a little more serious than your run of the mill display issue.

#64

webchick - April 4, 2009 - 15:58

So how about re-rolling and testing the patch, rather than re-iterating how much this needs to be fixed? :)

#65

Antinoo - April 6, 2009 - 03:29

#66

seanr - April 6, 2009 - 14:41

Webchick - I could do that for D6, but not D7 as I don't currently have PHP 5.2. Also, the testing errors give far too little information for me to successfully re-roll the patch. Why can we not see exactly which assertions failed? Failed 2 out of 50 tests is useless because I have no idea which the 2 are. BTW, my comment was largely a response to the person who previously downgraded this issue to normal - they seem not to understand the potential impact of this issue.

Antinoo - that is helpful, but it's a bad precedent to have to rely on a contributed module to fix a big bug in core. ;-) Besides, how many users will never even be aware this bug exists until after they've already gotten a ton of bad form submissions?

#67

geerlingguy - April 6, 2009 - 16:41

@ seanr / #66: can you set up a local LAMP/MAMP/WAMP environment, install D7 on top of PHP 5.2.x, then run the tests locally using the testing module? If you do that, you can see precisely which tests are failing.

#68

andypost - April 9, 2009 - 22:53

subscribe

#69

WiredEscape - April 11, 2009 - 18:44

subscribe

#70

netron - May 17, 2009 - 10:51

came across this bug on a D6 install i'm building for the client.

had to install the checkbox validate module

http://drupal.org/project/checkbox_validate

just something to be aware of if your client requires a legal "i agree to these terms and conditions" or a "i am over 16 years old" user register checkbox.

#71

davea - May 20, 2009 - 12:57

subscribe

#72

webchick - May 20, 2009 - 23:15

I'm inventing a tag for this. Would really appreciate even one, just one! of the people here having this bug taking a few minutes to re-roll and review this patch rather than repeatedly saying you have the same problem. We know. We need people to work on the patch to get it fixed.

#73

mgifford - June 12, 2009 - 05:23

Ack. This isn't a hard thing to do. Doesn't take 2-3 weeks to re-roll a patch.

Subscribe.

#74

luxx - June 12, 2009 - 17:16

subscribing

#75

noahb - June 13, 2009 - 20:20

here's a re-roll...

AttachmentSizeStatusTest resultOperations
checkbox_validate.patch10.47 KBIdleFailed: 11007 passes, 127 fails, 30 exceptionsView details | Re-test

#76

noahb - June 13, 2009 - 20:20
Status:needs work» needs review

#77

System Message - June 13, 2009 - 20:30
Status:needs review» needs work

The last submitted patch failed testing.

#78

JoepH - June 28, 2009 - 11:41

subscribing

#79

andypost - July 1, 2009 - 12:01

A lot of tests broken, patch needs re-roll

#80

evilgenius - July 26, 2009 - 14:31

Subscribing... I gotta stick with checkbox_validate module for the meantime.

#81

jromine - August 20, 2009 - 04:12

re-rolling this patch.

AttachmentSizeStatusTest resultOperations
form-179932-81.patch11.41 KBIdleFailed: 11922 passes, 127 fails, 58 exceptionsView details | Re-test

#82

geerlingguy - August 20, 2009 - 04:41
Status:needs work» needs review

Setting status for the testbot.

#83

System Message - August 20, 2009 - 07:45
Status:needs review» needs work

The last submitted patch failed testing.

#84

jromine - August 23, 2009 - 01:36
Status:needs work» needs review

try again

AttachmentSizeStatusTest resultOperations
form-179932-84.patch13.79 KBIdleFailed: 12246 passes, 124 fails, 58 exceptionsView details | Re-test

#85

System Message - August 23, 2009 - 01:50
Status:needs review» needs work

The last submitted patch failed testing.

#86

jromine - August 23, 2009 - 16:28
Status:needs work» needs review

Updated patch

AttachmentSizeStatusTest resultOperations
form-179932-86.patch12.78 KBIdleFailed: Failed to apply patch.View details | Re-test

#87

jcmarco - September 3, 2009 - 10:16

Wow! That's great! Congratulations!

This is one of the historic bugs in drupal for long time!

I have backported it to D6.13 and it works also great.

I was testing it with forms and even with the legal module without the checkbox_validate module that was patching this bug for long time.
Now, you always see the required field asterisk and stop you to send the form without the required field.

AttachmentSizeStatusTest resultOperations
form_checkbox_validate.patch3.37 KBIdleFailed: Failed to apply patch.View details | Re-test

#88

System Message - September 3, 2009 - 10:30
Status:needs review» needs work

The last submitted patch failed testing.

#89

jcmarco - September 3, 2009 - 11:14
Status:needs work» needs review

Umm! something wrong with the bot, that is a backporting to D6.13 to ease testing it.
Sorry, I didn't read the insurance contract letter size in the attachment form.

#90

jcmarco - September 3, 2009 - 11:15
AttachmentSizeStatusTest resultOperations
form_checkbox_validate-D6.patch3.37 KBIgnoredNoneNone

#91

mgifford - September 3, 2009 - 12:43

Patch needs to be built from root not from the includes directory. Patch #90 is going to fail too unfortunately.

But the code also doesn't work against core:

bash-3.2$ cvs update includes/form.inc
cvs update: warning: `includes/form.inc' was lost
U includes/form.inc
bash-3.2$ patch -p0 < form_checkbox_validate-D6.patch
can't find file to patch at input line 3
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|--- form.inc.orig 2009-07-22 14:21:19.328125000 +0200
|+++ form.inc 2009-09-03 11:15:20.234375000 +0200
--------------------------
File to patch: includes/form.inc
patching file includes/form.inc
Hunk #1 succeeded at 787 (offset 112 lines).
Hunk #2 FAILED at 1285.
Hunk #3 FAILED at 1990.
Hunk #4 succeeded at 2064 with fuzz 2 (offset 127 lines).
2 out of 4 hunks FAILED -- saving rejects to file includes/form.inc.rej

#92

jcmarco - September 3, 2009 - 13:50

Yes, it was wrong. First core patch rookie.
Once again, this is just a backporting to D6 from #86

AttachmentSizeStatusTest resultOperations
form_checkbox_validate_2-D6.patch3.38 KBIgnoredNoneNone

#93

mgifford - September 3, 2009 - 14:31

Ok, patch in #86 applies nicely.

What's stopping this from becoming RTBC?

What testing should be done? I'm running Simpletest now, I'm assuming it's all under the Form API

#94

DraeZ - September 4, 2009 - 02:25

This is great, It's awesome that this finally came together. I used the patch in post 92 For my Drupal 6 and it works great. Special thanks are definitely due for all the hard work.

#95

mgifford - September 4, 2009 - 12:53

The patch in #92 is for Drupal 6.

The patch in #86 is for Drupal 7.

We need more folks to review the patch for D7 before we can get it into core. At that point we can consider bringing this back to D6.

So who is going to do the final review of the D7 patch and mark it RTBC?

#96

seanr - September 10, 2009 - 20:08
Status:needs review» reviewed & tested by the community

I tested the patch in #86 and can confirm it works as expected. I created a simple module to test it and have attached it for anyone else who'd like to give it a try. The same module should work for Drupal 6 with an edit to the .info file.

AttachmentSizeStatusTest resultOperations
checkboxtest.zip863 bytesIgnoredNoneNone

#97

System Message - September 10, 2009 - 20:08
Status:reviewed & tested by the community» needs work

The last submitted patch failed testing.

#98

seanr - September 10, 2009 - 20:13
Status:needs work» reviewed & tested by the community

Bot appears screwed up because of #87. Should the patch from #86 be resubmitted?

#99

mgifford - September 10, 2009 - 20:44

I think it will be clear when the patch is being reviewed.

#100

chx - September 10, 2009 - 23:22

The patch reuses the integer 0 which already is unsupported as a checkboxes key. Good trick.

#101

webchick - September 10, 2009 - 23:40
Status:reviewed & tested by the community» needs work

Yay! Sign-off from the Form API maintainer! This is really close. I have some minor nits, mostly related to comments. Since this part of the code has been broken for eons, it'd be nice to document the way it's supposed to work once and for all.

+++ includes/form.inc 23 Aug 2009 16:26:03 -0000
@@ -754,7 +754,8 @@
+      // Unchecked checkbox has #value of numeric 0 (for now).

"for now"? I don't understand what that means. Please clarify.

+++ includes/form.inc 23 Aug 2009 16:26:03 -0000
@@ -1273,11 +1274,18 @@
+    if (!empty($element['#disabled'])) {
+      // Disabled controls are never successful.
+      // Always return default.
+      return $element['#default_value'];

Here again, I don't understand what that means. Successful? Meaning it's not validated, or..?

+++ includes/form.inc 23 Aug 2009 16:26:03 -0000
@@ -1273,11 +1274,18 @@
+      // Successful checkboxes are present with a value (possibly '0').
+      // http://www.w3.org/TR/html401/interact/forms.html#successful-controls
+
      // For an unchecked checkbox, we should return '' or maybe NULL, but for
+      // now we return numeric 0 since other core modules use a numeric database
+      // column for a checkbox value and assume its value will be 0 or 1.
+      return isset($input) ? $element['#return_value'] : 0;

Let's clean up these comments. They sound wishy-washy. Either we are returning zero or we're not, and we should explain why.

+++ includes/form.inc 23 Aug 2009 16:26:03 -0000
@@ -1957,17 +1965,20 @@
+  $t = get_t();
...
-    $checkbox = '<label class="option" for="' . $element['#id'] . '">' . $checkbox . ' ' . $element['#title'] . '</label>';
+    $required = !empty($element['#required']) ? ' <span class="form-required" title="' . $t('This field is required.') . '">*</span>' : '';
+    $checkbox = '<label class="option" for="' . $element['#id'] . '">' . $checkbox . ' ' . $element['#title'] . $required . '</label>';

This is really weird. None of the other element theme functions do this, no? Why are we doing it here?

+++ includes/form.inc 23 Aug 2009 16:26:03 -0000
@@ -2021,7 +2032,7 @@
-          '#default_value' => isset($value[$key]),
+          '#default_value' => isset($value[$key]) ? $key : NULL,
@@ -2134,7 +2145,7 @@
-            '#default_value' => isset($value[$key]),
+            '#default_value' => isset($value[$key]) ? $key : NULL,

Wait. NULL? I thought we returned 0? Why has this treatment not been given to other checkbox fields?

+++ modules/simpletest/tests/form.test 23 Aug 2009 16:05:14 -0000
@@ -72,42 +83,33 @@
+    $expected_values = array(
+      'disabled_checkbox_on' => 'disabled_checkbox_on',
+      'disabled_checkbox_off' => '',
+      'checkbox_on' => 'checkbox_on',
+      'checkbox_off' => '',
+      'zero_checkbox_on' => '0',
+      'zero_checkbox_off' => '',
+    );

Great tests! Awesome to have this in code so Drupal 7 is hopefully the /last/ release where we break checkboxes. :)

-9 days to code freeze. Better review yourself.

#102

jromine - September 12, 2009 - 20:20

I'll cleanup the comments, and re-roll the patch if needed.

An unchecked checkbox value should be represented by NULL, but I ran into issues elsewhere with this. Returning numeric 0 as the value of an unchecked checkbox is a compromise that fixes the issue at hand, and also doesn't break things elsewhere.

In form_process_checkboxes() and form_process_tableselect():

+++ includes/form.inc 23 Aug 2009 16:26:03 -0000
@@ -2021,7 +2032,7 @@
-          '#default_value' => isset($value[$key]),
+          '#default_value' => isset($value[$key]) ? $key : NULL,
@@ -2134,7 +2145,7 @@
-            '#default_value' => isset($value[$key]),
+            '#default_value' => isset($value[$key]) ? $key : NULL,

We're looking at a set of related checkboxes, and iterating through #options. $key could be (numeric) 0 for the first item. NULL for #default value represents the checkbox being unchecked. This matches how this is handled in form_process_radios().

I think the special theme handling is because checkboxes don't have a label above the form control.

#103

jromine - September 12, 2009 - 20:36
Status:needs work» needs review

patch attached

AttachmentSizeStatusTest resultOperations
form-179932-103.patch12.69 KBIdleFailed: Failed to apply patch.View details | Re-test

#104

chx - September 14, 2009 - 17:02
Status:needs review» reviewed & tested by the community

I think I like this :)

#105

System Message - September 18, 2009 - 07:05
Status:reviewed & tested by the community» needs work

The last submitted patch failed testing.

#106

mgifford - September 18, 2009 - 13:02
Status:needs work» needs review

Think this brings this patch up to the CVS.

AttachmentSizeStatusTest resultOperations
form-179932-106.patch7.09 KBIdleFailed: 12983 passes, 9 fails, 0 exceptionsView details | Re-test

#107

System Message - September 18, 2009 - 13:20
Status:needs review» needs work

The last submitted patch failed testing.

#108

mgifford - September 18, 2009 - 15:18

Hmm.. Rolled that patch wrong (more coffee in me now). Hopefully that's the problem, but these are the 9 fails.

Found the Submit button Other form.test 94 FormsAPIUnitTest->testCheckBoxProcessing() Fail
Found the requested form fields at form-test/checkbox Other form.test 94 FormsAPIUnitTest->testCheckBoxProcessing() Fail
A required checkbox is actually mandatory Other form.test 95 FormsAPIUnitTest->testCheckBoxProcessing() Fail
Checkbox 'disabled_checkbox_on' returns expected value (expected: 'disabled_checkbox_on', got: '<') Other form.test 111 FormsAPIUnitTest->testCheckBoxProcessing() Fail
Checkbox 'disabled_checkbox_off' returns expected value (expected: '', got: '<') Other form.test 111 FormsAPIUnitTest->testCheckBoxProcessing() Fail
Checkbox 'checkbox_on' returns expected value (expected: 'checkbox_on', got: '<') Other form.test 111 FormsAPIUnitTest->testCheckBoxProcessing() Fail
Checkbox 'checkbox_off' returns expected value (expected: '', got: '<') Other form.test 111 FormsAPIUnitTest->testCheckBoxProcessing() Fail
Checkbox 'zero_checkbox_on' returns expected value (expected: '0', got: '<') Other form.test 111 FormsAPIUnitTest->testCheckBoxProcessing() Fail
Checkbox 'zero_checkbox_off' returns expected value (expected: '', got: '<') Other form.test 111 FormsAPIUnitTest->testCheckBoxProcessing() Fail

AttachmentSizeStatusTest resultOperations
form-179932-107.patch11.13 KBIdleFailed: 12995 passes, 9 fails, 0 exceptionsView details | Re-test

#109

mgifford - September 18, 2009 - 15:19
Status:needs work» needs review

not quite enough coffee to change status to needs review.

#110

System Message - September 18, 2009 - 15:35
Status:needs review» needs work

The last submitted patch failed testing.

#111

elarifr - October 17, 2009 - 14:43

Hello
In drupal 6.14 i tried also checkbox #element_validate in author_contact module and it seems the function is not called while working with textfiled
it seems not only #required is buggy in checkbox
or maybe it is me :(

here are some code i played with just to see if it will display form_set_error

    $form['sendercheckbox1'] = array(
        '#title' => t(variable_get('authorcontact_form_checkbox1', 'I Agree')),
        '#type' => 'checkbox',
        '#return_value' => "Ok",
        '#required' => true ,
'#element_validate' => array('authorcontact_form_checkbox1_validate'),
    );

and
//to check element_validate not working with checkbox
//function authorcontact_form_name_validate($form_id, $form_values) {
//    //check the email address is valid
//    if(isset($form_values['values']['sendername'])) {
//        if (!valid_email_address($form_values['values']['senderemail'])) {
//            form_set_error('name', t('The email address you entered is not valid. '. variable_get('authorcontact_form_checkbox1', '') ));
// }
// }
//}
function authorcontact_form_validate($form_id, $form_values) {
    //check the email address is valid
    if(isset($form_values['values']['senderemail'])) {
        if (!valid_email_address($form_values['values']['senderemail'])) {
            form_set_error('email', t('The email address you entered is not valid'));
}
    }
}
function authorcontact_form_checkbox1_validate($form_id, $form_values) {
    //check the mandatory checkbox is checked
//    if($authorcontact_form_checkbox1 != "itmustfail") {
        if($form_values['values']['sendercheckbox1'] != "Ok") {
            form_set_error('', t('You must check the box authorcontact_form_checkbox1. '. variable_get('authorcontact_form_checkbox1', '') ));
        }
//    }

#112

sun - November 13, 2009 - 23:08
Status:needs work» needs review

Re-rolled #103 against HEAD + cleaned up the code.

AttachmentSizeStatusTest resultOperations
drupal.checkbox-required.112.patch12.67 KBIdlePassed on all environments.View details | Re-test
 
 

Drupal is a registered trademark of Dries Buytaert.