1

In the user registration form, I have set a certain field say surname as hidden i.e. I have unchecked the checkbox saying "Display on user registration form." as shown below:

enter image description here

Now I want to disable the server side validation for this field. When I print the $form array, the surname field is still displayed. However I am not able to find any such value in the $form array which checks if the value is unchecked or not. Is there any way to figure out if the said field is not displayed on the registration form? Hope I am clear enough.

1
  • Check the #access key on the element render array and it's children, that usually determines whether a field is going to be rendered or not
    – Clive
    Jan 30, 2017 at 19:01

1 Answer 1

0

If you look at the code of user_register_form(), you will notice it uses the following code.

  foreach (field_info_instances('user', 'user') as $field_name => $instance) {
    if (empty($instance['settings']['user_register_form'])) {
      $form[$field_name]['#access'] = FALSE;
    }
  }

If you are implementing hook_form_alter() (or other similar hooks), it's quite easier to look at the #access property. If you need that in other functions, you can use code similar to the following one.

  foreach (field_info_instances('user', 'user') as $field_name => $instance) {
    if (empty($instance['settings']['user_register_form'])) {
      // $field_name is not supposed to be added to the account registration form.
    }
  }

Clearly, checking for the #access property in a hook_form_alter() implementation is the safer method, since it consider the field can be made accessible from third-party modules (or third-party modules can hide a field that should be visible).
The other method is more for getting a specific field setting.

Your Answer

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

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