Created
December 29, 2011 00:11
-
-
Save nicksantamaria/1530610 to your computer and use it in GitHub Desktop.
Screwing around with 'An illegal choice has been detected. Please contact the site administrator.' error messages in Drupal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Validation handler for blah_node_form | |
*/ | |
function wpf_ui_blah_node_form_validate(&$form, &$form_state) { | |
$errors = form_set_error(); | |
// This ensures that empty select responses are dealt with correctly | |
$error_text = 'An illegal choice has been detected. Please contact the site administrator.'; | |
$empty_val = 'EMPTY'; | |
$count = 0; | |
foreach ($errors as $key => $error) { | |
if ($error == $error_text) { | |
$keys = explode('][', $key); | |
$formpart = $form; | |
$values = $form_state['values']; | |
foreach ($keys as $i) { | |
$formpart = $formpart[$i]; | |
$values = $values[$i]; | |
} | |
if ($formpart['#type'] == 'select') { | |
if (is_array($values) && isset($values[0]['tid'])) { | |
$values = $values[0]['tid']; | |
} | |
if ($values == $empty_val) { | |
$errors[$key] = t('%field field is required.', array('%field' => $formpart['#title'])); | |
$count++; | |
} | |
} | |
} | |
} | |
// Removing the unnecessary messages | |
foreach ($_SESSION['messages']['error'] as $key => $message) { | |
if ($message == $error_text && $count > 0) { | |
unset($_SESSION['messages']['error'][$key]); | |
$count--; | |
} | |
} | |
// Resetting and re-adding necessary error messages | |
drupal_static('form_set_error', array(), TRUE); | |
foreach ($errors as $key => $error) { | |
form_set_error($key, $error); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment