Does anyone know if it’s possible to have form_set_error() apply to multiple fields?

<?php
if((condition1) || (condition2)) {
 
form_set_error(‘conditionfield1’,’conditionfield2’,t(‘Wow, what an error’));
}
?>

Or maybe something like…

<?php
if((condition1) || (condition2)) {
 
form_set_error(‘conditionfield1’, t(‘Wow, what an error’));
 
form_set_error(’conditionfield2’, t(‘Wow, what an error’));
}
?>

Does anything like this make sense?
Thanks

Comments

Hmm...

Interesting question. I don't see what you couldn't at least do the second method. I'll be interested in what other answers come up.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

not tested, but it should

not tested, but it should work...

<?php
if (condition) {
 
form_set_error(NULL, t(‘Wow, what an error’));
 
form_set_error('conditionfield1');
 
form_set_error('conditionfield2');
}
?>

José San Martin
http://www.verinco.com/
http://www.gare7.org/
http://www.studying-linguistics.com/

José San Martin
http://www.chuva-inc.com/

Worked.

That did work, thanks!

There might have been some weird issues with resolution after the error message, but the initial error display worked fine. I say 'might' because we ended up changing our logic, yielding that condition useless, so I did not test it further. But thanks for your help!

one error message, multiple fields

I came across this question today.

I'm not sure what version you're referring to, but under Drupal 5.x your (albeit untested) example doesn't work. If you look at the code for form_set_error it's easy to see why. Setting the first name parameter to NULL will cause isset() to return false. Also, not defining the second message parameter will prevent drupal_set_message() from firing.

According to the documentation, form_set_error is suppose to allow setting errors for multiple fields by grouping them together as child elements, but I wasn't able to get this to work either.

...

Yeah, the above example is not supposed to work.

Could you please open a feature request?

before creating a feature request

I'd be curious to know if anyone has successfully used form_set_error() according to the documentation.

If you mean...

If you mean in the standard manner (one error, one field, one message), yes - there are many examples throughout core as well.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

And if you mean...

form_set_error is suppose to allow setting errors for multiple fields by grouping them together as child elements, but I wasn't able to get this to work

It works. Perhaps you didn't use #tree => TRUE on the parent element?

(There's one small error in the doc: Nowadays elements aren't named edit[foo][bar] but simply foo[bar]. But don't mention this in your issue; it isn't relevant.)

I mean the latter

That is: one error, multiple fields, one message.

I have the #tree attribute set to TRUE.

I also noted the syntax error.

Still no success. I've moved on to other more important project tasks, but will revisit this later.

dirty solution

Well, after lot's of testing, I find a solution, while not very nice, it just works.

The above code didn't worked for me (Drupal 6 right now) as when adding NULL to the error won't print and fields won't get marked if an error message is not passed (neither with '' as message), so I fixed it passing a blank space as message.

This worked for me:

<?php
if (condition) {
 
form_set_error('conditionfield1', t(‘Wow, what an error’));
 
form_set_error('conditionfield2', ' ');
}
?>

Now I have the two fields marked as error and just a blank list entry on the errors list... can live with it.. :)

Hopes this helps anybody.

interesting

botum, thanks for sharing your findings.

I discovered another technique for displaying multiple error messages is to use form_set_error for the first message and drupal_set_message (with the second parameter set to 'error') for any additional messages. This won't solve the problem you're talking about though.

form_set_error and multiple fields

up. I've this problem, too. I'd like to get a single error message, while highlighting two or more fields. Is it possible to do that?

I tried also with form_set_error( array('FirstField', 'SecondField'), t('FF has to be > than SF!') ) but it doesn't work...

No solution for this (common?) question?

time to revisit this issue and find a solution

So nearly 1 1/2 years later since the original post of this thread the inability to display one error message for multiple fields still persists.

I need the ability to highlight (i.e. red outline style by default) multiple fields while only displaying one error message. For example:

<?php
  form_set_error
('address1', t(‘Please complete all the required address fields.));
 
form_set_error('city');
 
form_set_error('state');
 
form_set_error('postal');
}
?>

Currently in the above example only the 'address1' field is highlighted.

Interesting excluding the second message string argument from form_set_error() still allows the fieldname to be added to the static form array, but the field highlighting is ignored.

<?php
function form_set_error($name = NULL, $message = '') {
  static
$form = array();
  if (isset(
$name) && !isset($form[$name])) {
   
$form[$name] = $message;
    if (
$message) {
     
drupal_set_message($message, 'error');
    }
  }
  return
$form;
}
?>

Yeah, I can define the second argument as a space like so

<?php
  form_set_error
('postal', ' ');
}
?>

but this is a lame workaround and adds extra space to the error message div.

-------------------------------------------------------

"If you don't read the newspaper you are uninformed;
if you do read the newspaper you are misinformed."
-- Mark Twain

Example of a solution

Alex Comer
Platinum Code
www.platinumcode.com

I'm pretty sure the above

I'm pretty sure the above solution only works if the password fields are the only fields being passed into password_confirm_validate().

If you don't want to set the two, or more, comparative form elements under a common parent (looking at the code, this is what seems to make form_error() really work), I suggest setting the first element with form_set_error() as explained a bit further above, and then setting a flag value to rebuild the attributes of the other form elements.

Looking at the source code, I found that the error highlighting around form fields is turned on by adding the class 'error' to the form element.

So starting within your hook_validate function with form elements 'email' and 'confirm_email':

<?php
function myform_form_validate ($form, &$form_state) {
   
$error = FALSE;
   
// ...any other validation checks, making sure that $error is set to true if a validation check fails

    // If there are any previous validation errors, error message will exist -> set $error = TRUE
   
$errors = form_get_errors();
    if (!empty(
$errors)) {
       
$error = TRUE;
    }

    if (
$form_state['values']['email'] != $form_state['values']['confirm_email']) {
       
form_set_error('email', 'Email verification failed, please try again.');
       
$form_state['rebuild'] = TRUE;
       
$form_state['whitepaper']['confirm_email_error'] = TRUE;
    } else {
        if (
$error) {
           
$form_state['rebuild'] = TRUE;
        }
       
$form_state['whitepaper']['confirm_email_error'] = FALSE;
    }
}
?>

The key above being setting $form_state['rebuild'] = TRUE, after setting your flag value to be 1. Also, we need another sentinel value $error to mark if there are any other errors in the validation process. Because $form_state['rebuild'] ignores any $form_state['redirect'] value, the $error needs to be turned on. That way if there are any previous failures in validation the form will rebuild itself, and re-present itself to the user. Otherwise, the form will proceed as expected.

Then in your hook_form:

<?php

function myform_form($form_state) {
   
$form['name'] = array(
       
'#type' => 'textfield',
       
'#title' => t('Full Name'),
       
'#required' => TRUE,
       
'#default_value' => (isset($form_state['values']['name'])) ? $form_state['values']['name'] : '',
    );

   
// ...additional form fields

   
$form['email'] = array(
       
'#type' => 'textfield',
       
'#title' => t('Email Address'),
       
'#required' => TRUE,
    );
   
    if (isset(
$form_state['mymodule']['confirm_email_error']) && $form_state['mymodule']['confirm_email_error'] === TRUE) {
       
$attributes = array('class' => 'error');
    } else {
       
$attributes = array();
    }
   
   
$form['confirm_email'] = array(
       
'#type' => 'textfield',
       
'#title' => t('Confirm Email'),
       
'#required' => TRUE,
       
'#attributes' => $attributes,
    );
   
   
// ... any other fields
   
   
return $form;
}
?>

Setting $form_state['rebuild'] = TRUE will allow you to reset the attributes after validation. For additional form fields, just set '#atttributes' => $attributes.

I included the form element 'name' to illustrate that any values that you want repopulated when the form rebuilds, you have to use the '#default_value' property for the form element, otherwise the value is not populated.

Not all pretty, but does work.

Hate to bring up old stuff but...

Has this changed? I'm doing something similar to this, but I can't seem to get past form.inc line 141 if I set an error - it doesn't rebuild the form, it just re-renders it, which doesn't seem to give my form function a chance to do anything differently...

drupal-by-david.com

Sometimes trying to do things

Sometimes trying to do things 'The Drupal Way' seems just waaay too much work, assuming it's even possible. I just use something like this. Crude. Works.

form_set_error('element_name', t('Error'));
array_pop($_SESSION['messages']['error']);

If your repeated errors are not sequential, you'll need to expand on that a bit...

Yep, waaay too hard :)

John,

I agree with the waaay too much work sometimes. Your solution worked fine thanks, just popped every message as I created them and then did the below to get an actual message up.

<?php
 
if (form_get_errors()) {
   
drupal_set_message($msg, 'error');
  }
?>

Anthony.

Popping all but one makes

Popping all but one makes more sense to me.

Better yet

Simply filter the array to remove duplicates.

<?php
$_SESSION
['messages']['error'] = array_filter($_SESSION['messages']['error']);
?>

I think you

I think you mean

<?php
$_SESSION
['messages']['error'] = array_unique($_SESSION['messages']['error']);
?>

which would remove all duplicate error messages. I recently used this to solve the problem described in this thread.

any example to that

i am strggling to do that. can anyone give me example for that. i am really struggling to get this done.

solution found folks

set #parents => array('foo'), on each element that is to be highlighted together (in my case i require either a telephone number or an email address for my form)

in your validation/submission function put form_set_error($name = 'foo', $message = 'how much do you all love me?');

don't forget

don't forget to apply $form['#tree'] = TRUE

The array_pop method would

The array_pop method would remove other error messages, so that can't be assumed "safe".

The message above about setting #parent would certainly work, but you'd set errors on every child, even those that contain data and are already correct.

I think the best thing to do is run <?php $_SESSION['messages']['error'] = array_unique($_SESSION['messages']['error']); ?> at the end of your _form_validate() function after setting errors on everything that's an error.

Edit: Well, you'd really want to make sure the ['error'] array exists before setting it, otherwise you'll print errors even when there are none!

<?php
// Remove duplicate messages.
if (isset($_SESSION['messages']['error'])) {
 
$_SESSION['messages']['error'] = array_unique($_SESSION['messages']['error']);
}
?>
nobody click here