How to theme the node edit form to group fields into collapsible containers? I'd like to tidy up an unwieldy node edit form using collapsible fieldset elements.

I can do this by implementing a form_alter hook but will this affect Drupal/other modules' ability to process/validate the fields? Obviously it will change the #parents of several elements, and I'm unclear whether this change will also affect $form_state['values'].

Is it safe to do this, or is there another way to achieve the same?

Example code

$form['group1'] = [
  '#type' =&gt; 'fieldset',
  '#title' =&gt; 'My Grouped Fields',
  '#collapsible' =&gt; TRUE,
  '#collapsed' =&gt; TRUE,
  'children' =&gt; [
    'body' =&gt; $form['body'],
    'field_smallprint' =&gt; $form['field_smallprint']
    ],
  ];
unset($form['body'], $form['field_smallprint']);


It seems to me that this is probably safe unless #tree is TRUE, but I don't want to implement something that might lose data, hence the question here.
I would recommend you to first try the Field Group module that provides the option to do that kind of grouping and many more options.