Rearrange fields using alterFormDefinition

Let’s say we want to rearrange some fields. Or perhaps we want to move some fields into another fieldset. The following example covers that case:

var rearrangeFields = function(args) {
  // First add a new fieldset to hold these fields
  var name_fieldset = {
      name: 'Name',
      title: 'Name',
      type: 'fieldset',
      children: []
  var name_index = args.form_definition.form_elements.push(name_fieldset);
  // Decrement as push returns the length, we want reference to the last element

  _.each(args.form_definition.form_elements, function(child) {
    if ( === 'ContactInformation') {
        _.each(child.children, function(element, key, children) {
            if ( === 'FirstName' || === 'LastName') {
                // Add to our new name fieldset

                // Remove from the other fieldset
                children[key] = { 'type': 'hidden'};
        child.title = 'Address';
  return args;

In this case we are creating a new fieldset for Name, moving the name fields into that fieldset, removing them from their original fieldset (ContactInformation), and changing the title of the Contact Information fieldset to Address to better indicate its purpose.