drupaldrupal-7drupal-formsform-apihook-form-alter

alter "required" property of the form element in drupal 7


I have some require fields in some form,I want them be required in somewhere and in another place not be required (I want exactly them , maybe you suggest me use existance field in cck but this fields not in cck.) I used profile2 module with some profile type, I want in profile type edit page as example name field be required and in user registration name field not be required. I try to alter #require property of name field in form alter, it's changed correctly but in validation tell me the name field is required.

in

      function mymodule_form_alter(&$form,&$form_state) {
            if ($form_id == 'user_register_form') {
              $form['profile_detailp']['field_name']['und']['#required']=0;
              ...
              }

      }

any idea ?


Solution

  • Your method is ok, and have to works but maybe in another module it require again.

         function mymodule_form_alter(&$form,&$form_state) {
            if ($form_id == 'user_register_form') {
              $form['profile_detailp']['field_name']['und']['#required']=0;
              ...
              }
    
      }