4

When a user submits a content that has a taxonomy selection field and NOT make a selection, then the referenced error appears.

Now, I know exactly when, how and where the message appears, is there any way to change it so that it's actually useful for the user (i.e. "You did not select a category."), and not merely a cryptic error message that could potentially freak the user out?

1

I would add custom validation to the node, so that you can control the error message that get's output. There's an excellent example of that in this question. A potential problem with this is that you may now get both error messages. I don't immediately have a suggestion for how to deal with that.

  • Okay, I was actually being lazy--I knew this was one way to go about it (and in fact, did this for other errors), but I didn't want to roll yet another custom validator. I was hoping there was an easier way. I'll submit my own answer and provide a way to remove the cryptic message and only show the error that should be shown. – Peter Jan 8 '12 at 21:04
6

This is to add onto Letharion's answer. Using the same principle, we can also check for error messages to replace in the validator.

function MY_MODULE_form_validate($form, &$form_state){
    $errors = drupal_get_messages(); // (dpm()'s has to go AFTER this line--they get cleared)
    foreach ($errors as $type => $id) {
        foreach ($id as $message){
            // Loop through individual messages, looking for ones to remove or replace
            if (test_for_invalid_error($message)===FALSE){
                drupal_set_message($message,$type);
            } elseif (test_for_invalid_error($message)!==TRUE){
                drupal_set_message(test_for_invalid_error($message), $type);
            }
        }
    }
}

function test_for_invalid_error($message){
    if (strpos($message, 'An illegal choice has been detected. Please contact the site administrator.') !== false) {
        return t('Please select a category!');
    }
    return FALSE;
}

This will not only add your own custom error, but replace the "An illegal choice" error. Isn't it neat? Yay!

1

The module String Overrides will be yours best solution , just change the string for what ever you like.

  • That would replace the text string globally, potentially giving a very confusing error message in every single other situation where this message is used. – Letharion Jan 8 '12 at 11:56
  • ok , but it give him the chance to write some other message for the user that will not freak the user out. – witchcraft Jan 8 '12 at 12:03
  • I see what you mean, but disagree with your conclusion. Later, a user will make a mistake with say, a Views exposed filter, Views tries to output the same error message, the user get's an error message that's about taxonomy terms, and is only useful in the context of node-edit forms. Now that will freak out the user. In my opinion, this solution, will multiply the problem, instead of remove it. – Letharion Jan 8 '12 at 12:07
  • got your point. – witchcraft Jan 8 '12 at 12:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.