0

Drupal 7 is not displaying highlighted fields on using form_set_error but error message is getting displayed. I have rendered my form hook_form in template. Could that be the cause for this issue?

Here is my code:

if(($form_state['values']['field1']=='' ){
 form_set_error('', t('one field is required')); 

 }

2 Answers 2

5

First argument of form_set_error tells Drupal what element should get highlighted. Obviously empty string means "none", so your code works exactly as designed.

To make field required, you can go a simpler way: Use #required attribute and let Drupal take care for details.

To round it off, as Clive said, your code would be:

form_set_error('field1', t('one field is required'));

But if you're just checking for an empty field, use #required.

1
  • To round it off @user25283 your code would be form_set_error('field1', t('one field is required'));. But if you're just checking for an empty field, use #required as Mołot says
    – Clive
    Commented Jan 15, 2014 at 13:06
0

It's not working for me using the below code:

  $form['field1'] = array(
        '#title' => t('Field1'),
        '#size' => 24,
        '#type' => 'textfield',
    '#default_value' =>$field ,
    '#required' => TRUE,      
  '#element_validate' => array('function_name'), 


  );

if(($form_state['values']['field1']=='' ){
 form_set_error('field1', t('one field is required')); 

 }

getting the error message in red at the top but my text box is not getting highlighted with red border. I have used this text box in hook_form in a module, but i have called this form in theme using

print render(drupal_get_form('hook_form'));

I guess the error message is not getting highlighted since am calling this in a theme.

This is getting worked when i call my form in a module but i want the form to be called from a template. In this case my error message is not getting highlighted with red border over the text box.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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