Setting the form value to an organic group gid from validation hook of custom field widget (Drupal 7) I have a custom widget which can be used for a group audience field.  Instead of being a dropdown like the OG_AUDIENCE_WIDGET it is made up of three multiple select list, and the group which is actually selected is the one which is picked in the third select list.  I have the actual widget working but I am stuck on validation.  I have been trying to set the gid in a couple of different ways which have failed in a couple different manors.  First:

function multiselect_validate($element, &amp;$form_state)
{
  if($form_state['submitted'])
  {
    $index = array_shift(array_values($element['wrapper']['dropdown_assignment']['#value']));
    $assignment_name = $element['wrapper']['dropdown_assignment']['#options'][$index];
    $query = new EntityFieldQuery();
    $result = $query
    -&gt;entityCondition('entity_type', 'node')
    -&gt;entityCondition('bundle', 'assignment_group')
    -&gt;propertyCondition('title',$assignment_name)    
    -&gt;execute();
    $node  = reset($result['node']);
    $assignment_gid = og_get_group("node", $node-&gt;nid)-&gt;gid;
    $value = $value[LANGUAGE_NONE][0]['gid'] = $assignment_gid;
    form_set_value($element,array('gid'=&gt;198), $form_state);
  }
}


This kind of validation returns an error:  

[11-Dec-2012 11:23:07] PHP Fatal error:  Cannot unset string offsets in /Applications/MAMP/htdocs/arcade/modules/field/field.default.inc on line 41


I also tried something like this for the final form_set_value:

form_set_value($element,array('gid'=&gt;$assignment_gid), $form_state);


But when I do this and then look at the result nothing actually gets set into the field.

What is the proper way to set the gid for the field?
I'm assuming the validate callback that you have mentioned in your question is the #element_validate callback for the field widget element.

In that case, form_set_value must be set to a list of items because a field is inherently multi-valued. Each item in this list must conform to the schema of the field.

So, in this case:

$items = array();
$delta = 0;
$gid = ...; // From somewhere
$items[$delta] = array(
  'gid' =&gt; $gid,
  'state' =&gt; OG_STATE_ACTIVE, // optional
  'created' =&gt; REQUEST_TIME - 3600 // optional (yesterday)
);
form_set_value($element, $items, $form_state);


To illustrate this better, I have a sandbox project up on drupal.org. It declares a new widget with hierarchical dropdowns just like you have described. You can check the code directly from http://drupalcode.org/sandbox/amarnus/1868742.git/tree/refs/heads/master or clone the project from http://drupal.org/sandbox/amarnus/1868742. Hope this helps!