Field collection Config (multi value)

Field1---Name -Autocomplete (single value) (Entity Reference)
Field2---Score - Computed field.

Example Values for field collections

Field collection [0]
------Bill (1112)- where 1112 is nid
------Score - some value will be generated depends on 1112

Field collection [1]
------Roger (1345)- where 1345 is nid
------Score - some value will be generated depends on 1345

Field collection [2]
------Roger (1467)- where 1467 is nid
------Score - some value will be generated depends on 1467
..
..
Field collection [n]
------Maddy (1652)- where 1652 is nid
------Score - some value will be generated depends on 1652

From above config how to make field Name's value should not hold the duplicate value for field collection ie Bill should not be applied again.
There is a module called https://drupal.org/project/unique_field which forces field's value to be unique per content type or per all node, but not per field.
In above case somehow(that what is required) if we make per field then also it could only applied to Name field which is inside field collection rather than field collection itself. Right?

up vote 5 down vote accepted

I had to do this the other day and found it easiest with a custom validation function.

Using hook_form_alter add a new validation function:

function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id) {
  //drupal_set_message("Form ID is : " . $form_id);
  switch($form_id) {
    case 'YOUR_FORM_ID':
      //dpm($form);
      $form['#validate'][] = 'validate_unique_field_collection_names';
      break;
  }
}

And then in your new validation function:

function validate_unique_field_collection_names($form, &$form_state) {
  $used_ids = array();
  foreach($form_state['values']['field_people_collection'][LANGUAGE_NONE] as $k => $item) {
    if($k === 'add_more') continue; // skip loop if key is add_more
    $user_target_id = $item['field_name'][LANGUAGE_NONE][0]['target_id'];
    if(in_array($user_target_id, $used_ids)) { // check if UID is in used array
      form_set_error('field_people_collection]['.LANGUAGE_NONE.']['.$k.'][field_name', t('Names must be unique.')); // if it is set an error
    } else {
      $used_ids[] = $user_target_id; // else add it into the used array
    }
  }
}
  • for time being just using your code by replacing hardcoded field name, also raised the feature request at D.O drupal.org/node/2231189 – kiranking Apr 3 '14 at 18:47

With this modification we can highlight the repeated fields.

        if(in_array($user_target_id, $used_ids->id)) { // check if UID is in used array

            form_set_error('field_people_collection]['.LANGUAGE_NONE.']['.$used_ids->k[$user_target_id].'][field_name');
            form_set_error('field_people_collection]['.LANGUAGE_NONE.']['.$k.'][field_name', t('Names must be unique.')); // if it is set an error

        } else {

            $used_ids->id[$user_target_id] = $user_target_id; // else add it into the used array
            $used_ids->k[$user_target_id] = $k;
        }

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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