Problem/Motivation

If a poll form is submitted without selecting any of the poll options, the #default_value = -1 for the choices element causes the error message "An illegal choice has been detected. Please contact the site administrator" to be produced instead of the proper error "Your vote could not be recorded because you did not select any of the choices."

Before screenshot

Proposed resolution

Remove the #default_value.

After screenshot

Remaining tasks

 

Contributor tasks needed
Task Novice task? Contributor instructions Complete?
Create a patch Novice Instructions Yes
Add automated tests Novice Instructions Yes
Manually test the patch Novice Instructions Yes
Embed before and after screenshots in the issue summary Novice Instructions Yes
Review patch to ensure that it fixes the issue, stays within scope, is properly documented, and follows coding standards Instructions Yes

User interface changes

API changes

Original report by @gargsuchi

I have Drupal 7.15 installed.
I am using panels to build my site - so my poll node is rendered using panel.
If I don't select any of the choices and press the vote button, I get this error:

An illegal choice has been detected. Please contact the site administrator.

Instead of the normal:
Your vote could not be recorded because you did not select any of the choices.

Any idea why this might be happening?

Files: 
CommentFileSizeAuthor
#15 after.png12.93 KBdcam
#15 before.png12.73 KBdcam
#14 1823906-14-ccombined.patch1.45 KBjoshi.rohit100
PASSED: [[SimpleTest]]: [MySQL] 41,039 pass(es).
[ View ]
#12 1823906-12-ccombined.patch1.72 KBjoshi.rohit100
PASSED: [[SimpleTest]]: [MySQL] 40,945 pass(es).
[ View ]
#9 1823906-9-tests.patch669 bytesamitgoyal
FAILED: [[SimpleTest]]: [MySQL] 40,949 pass(es), 1 fail(s), and 0 exception(s).
[ View ]
#5 1823906-5-poll-illegal-choice.patch1.12 KBrahulbile
PASSED: [[SimpleTest]]: [MySQL] 39,323 pass(es).
[ View ]

Comments

earnie’s picture

Project:Drupal core» Panels
Version:7.15» 7.x-3.x-dev
Component:poll.module» Miscellaneous

This could be an effect from panels, have you asked them?

drupalhooked’s picture

I also faced the same issue with poll module. I think the issue is not because of the panels module. I faced the issue with out panels.

I think issue is due to #default_value set to -1 for $form['choice'] inside function poll_view_voting() at line 723 in poll module . When form is being build by drupal's form api there is a check that #default_value should be one from the options for that form element.

Fix:
In your custom module you can fix it by implementing hook_form_FORM_ID_alter as below

function MYMODULE_form_poll_view_voting_alter(&$form, &$form_state, $form_id) {
        // Unset the default value which is causing issue
unset($form['choice']['#default_value']);
       
        // Define custom validate function to apply the validation
$form['#validate'][] = 'MYMODULE_form_poll_view_voting_validate';
}

function MYMODULE_form_poll_view_voting_validate($form, &$form_state){
  if (!isset($form_state['values']['choice'])) {
    form_set_error( 'choice', t('Your vote could not be recorded because you did not select any of the choices.'));
  }
}

lsolesen’s picture

Project:Panels» Drupal core
Version:7.x-3.x-dev» 7.15
Component:Miscellaneous» poll.module
Issue summary:View changes

Seems to be a poll issue - not a panels issue.

rahulbile’s picture

Assigned:Unassigned» rahulbile
Status:Active» Needs work
rahulbile’s picture

Assigned:rahulbile» Unassigned
Status:Needs work» Needs review
StatusFileSize
new1.12 KB
PASSED: [[SimpleTest]]: [MySQL] 39,323 pass(es).
[ View ]

Attached is the patch against 7.x.

rahulbile’s picture

Version:7.15» 7.x-dev
dcam’s picture

Issue summary:View changes
Status:Needs review» Needs work
Issue tags:+Needs tests, +Novice

This needs to have automated tests added. See the instructions for creating automated tests.

dcam’s picture

Issue summary:View changes
amitgoyal’s picture

Status:Needs work» Needs review
StatusFileSize
new669 bytes
FAILED: [[SimpleTest]]: [MySQL] 40,949 pass(es), 1 fail(s), and 0 exception(s).
[ View ]

Please review the attached patch for creating automated tests for this scenario.

Before applying the patch in #5, automated tests show 1 failure. After applying the patch in #5, automated tests show 0 failure.

Status:Needs review» Needs work

The last submitted patch, 9: 1823906-9-tests.patch, failed testing.

dcam’s picture

Great work, amitgoyal. I just have a few notes for you.

+++ b/modules/poll/poll.test
@@ -315,6 +315,11 @@ class PollVoteTestCase extends PollTestCase {
+    $this->assertText('Your vote could not be recorded because you did not select any of the choices.', 'An illegal choice has been detected. Please contact the site administrator.');

'Your vote could not be recorded because you did not select any of the choices.' should be wrapped in the t() function, since it should be translated on the page where it appears.

'An illegal choice has been detected. Please contact the site administrator.' isn't an appropriate assertion message and can be changed to be more descriptive about the thing you're asserting, for instance 'Found the empty poll submission error message.' You probably knew this, but it should not be wrapped in t().

Finally, I'll need to see #5 and #8 combined into one patch in order to do a proper review.

joshi.rohit100’s picture

Status:Needs work» Needs review
StatusFileSize
new1.72 KB
PASSED: [[SimpleTest]]: [MySQL] 40,945 pass(es).
[ View ]

combined patch of #5 and #10 with changes mentioned in #11.

dcam’s picture

Status:Needs review» Needs work

Thank you for working on the combined patch. Unfortunately, your patch contains changes to your .gitignore file that were added by mistake.

joshi.rohit100’s picture

Status:Needs work» Needs review
StatusFileSize
new1.45 KB
PASSED: [[SimpleTest]]: [MySQL] 41,039 pass(es).
[ View ]

sorry for the mistake. Updated the patch.
Please review now.

dcam’s picture

Title:Polls - An illegal choice has been detected. Please contact the site administrator. - in case I do not select a choice» Incorrect error message when poll is submitted with no option selected
Issue summary:View changes
Status:Needs review» Reviewed & tested by the community
Issue tags:-Needs tests
StatusFileSize
new12.73 KB
new12.93 KB

Alright! #14 is RTBC. The patch fixes the issue where the "Your vote could not be recorded..." error message did not appear when a poll is submitted without an option being selected.

Big thanks to everyone who helped work on this!

@David_Rothstein or any other core committer: the tests-only patch is in #9.

Status:Reviewed & tested by the community» Needs work

The last submitted patch, 14: 1823906-14-ccombined.patch, failed testing.

dcam’s picture

Status:Needs work» Reviewed & tested by the community

Status:Reviewed & tested by the community» Needs work

The last submitted patch, 14: 1823906-14-ccombined.patch, failed testing.

dcam queued 14: 1823906-14-ccombined.patch for re-testing.

dcam’s picture

Status:Needs work» Reviewed & tested by the community

Status:Reviewed & tested by the community» Needs work

The last submitted patch, 14: 1823906-14-ccombined.patch, failed testing.

dcam queued 14: 1823906-14-ccombined.patch for re-testing.

dcam’s picture

Status:Needs work» Reviewed & tested by the community

Status:Reviewed & tested by the community» Needs work

The last submitted patch, 14: 1823906-14-ccombined.patch, failed testing.

dcam queued 14: 1823906-14-ccombined.patch for re-testing.

dcam’s picture

Status:Needs work» Reviewed & tested by the community
David_Rothstein’s picture

Status:Reviewed & tested by the community» Fixed

Committed to 7.x - thanks!

  • David_Rothstein committed 0c8dc6e on 7.x
    Issue #1823906 by joshi.rohit100, amitgoyal, rahulbile | gargsuchi:...

Status:Fixed» Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.