How to dynamically set the maximum cardinality for a multi value field? I am using a multi value image field which is set to hold 5 values. What I need, is to be able to set a lower cardinality for this field on specific nodes of the same content type.

Edit: Below is what I ended up using, it might help someone.

/**
 * Implements hook_field_attach_form().
 */
function example_field_attach_form($entity_type, $entity, &amp;$form, &amp;$form_state, $langcode) {
  // Our field name
  $field_name = 'field_listing_images';

  // You may want to add more conditions
  if (isset($form[$field_name][LANGUAGE_NONE])) {

    // Our custom cardinality (hardcoded here)
    $custom_cardinality = 3;

    // Unset all field multi values that are equal or larger than our $custom_cardinality variable
    foreach ($form[$field_name][LANGUAGE_NONE] as $k =&gt; $value) {
      if ($k &gt;= $custom_cardinality) {
        unset($form[$field_name][LANGUAGE_NONE][$k]);
      }
    }

    // Our custom form validation
    $form['#validate'][] = 'example_custom_form_validate';
  }
}
/**
 * Our custom form validation callback.
 */
function example_custom_form_validate($form, &amp;$form_state) {
  // Our field name
  $field_name = 'field_listing_images';

  // You may want to add more conditions
  if (isset($form_state['values'][$field_name][LANGUAGE_NONE])) {

    // Our custom cardinality (hardcoded here)
    $custom_cardinality = 3;

    // Unset all field multi values that are equal or larger than our $custom_cardinality variable
    foreach ($form_state['values'][$field_name][LANGUAGE_NONE] as $k =&gt; $value) {
      if ($k &gt;= $custom_cardinality) {
        unset($form_state['values'][$field_name][LANGUAGE_NONE][$k]);
      }
    }
  }
}
My cardinality was based on the value of another field in a related entity, so I figured out that I could set the number of values to Unlimited in the field settings, then, in hook_form_alter, reattach the field to the form with a new cardinality:

$field = field_info_field('field_my_field');
$field['cardinality'] = $my_new_cardinality;
$instance = field_info_instance('entity_type', 'field_my_field', 'bundle');

// Depending on the form, the entity may be found in $form['#entity'],
// in $form_state, or elsewhere.
// $langcode is usually LANGUAGE_NONE, but may change in multilingual sites.
$items = isset($entity-&gt;field_my_field[$langcode]) ? $entity-&gt;field_my_field[$langcode] : array();
$new_field = field_multiple_value_form($field, $instance, $langcode, $items, $form, $form_state);

// This is not needed. It tricks theme_field_multiple_value_form into
// not adding the value reordering widget.
$new_field['#cardinality'] = 0;
foreach (element_children($new_field) as $delta) {
  $new_field[$delta]['_weight']['#access'] = FALSE;
}

$form['field_my_field'][$langcode] = $new_field;