1

I use Drupal 7.x. I add several fields in the user profile form.

In the registration i don't need them, but in the first login it must be filled. I can't use Profile2 module.

In my module:

function mymodule_form_user_profile_form_alter(&$form, &$form_state) {      
          $form['field_test_field']['#required'] = 'TRUE';   
}

The function is fired, but don't make it required.

I tried also:

$form['field_test_field'][0]['#required'] = 'TRUE';  
$form['#field_info']['field_test_field']['required'] = '1'; 

How can make an user_profile_form field Required in the form alter?

nmc
  • 8,724
  • 5
  • 36
  • 68
Joseph
  • 145
  • 4
  • 16

3 Answers3

2

The following example can be used for text field:

$form['field_full_name'][LANGUAGE_NONE]['0']['value']['#required'] = TRUE;

Second example is for email field:

$form['field_custom_email_address'][LANGUAGE_NONE]['0']['email']['#required'] = TRUE;

Rasim
  • 1,276
  • 1
  • 11
  • 24
0

You're currently setting #required to a string (using single quotes) instead the boolean it needs to be. Give this a go $form['field_test_field']['#required'] = true;

Sean3z
  • 3,745
  • 6
  • 26
  • 33
0

I found the solution: How to disable a field or make it readonly in Drupal 7

$form['field_secured_title']['und']['0']['value']['#attributes']['disabled'] = TRUE;

This worked.

But if i use multi language will it still work?

sorry for my English.

Community
  • 1
  • 1
Joseph
  • 145
  • 4
  • 16
  • The `und` key defines the language (currently undefined). In fact, it's probably better if you use the constant `LANGUAGE_NONE` instead of `und`. This is what you would need to change if you are using a different language. – nmc Oct 04 '12 at 01:38