Solution1 Limit on the form:
function <module>_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'center_node_form') {
global $user;
// Only allow to upload 2 images for role = staff
if (in_array('staff', $user->roles)) {
$center_image_limit = 2;
$form['#field_info']['field_center_image']['multiple'] = $center_image_limit;
$i = 1;
foreach ($form['field_center_image'] as $key => $value) {
if (is_numeric($key)) {
if ($i > $center_image_limit) {
unset($form['field_center_image'][$key]);
}
$i++;
}
}
}
}
}
Source: DRUPAL – LIMIT THE NUMBER OF VALUES OF A CONTENT TYPE FIELD BY ROLE @ 1
Solution2 Limit in validation:
function <module>_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'center_node_form') {
// Add the extra validation
array_unshift($form['#validate'], '<module>_addtional_validate');
}
}
function <module>_addtional_validate($form, $form_state) {
$count = 0;
foreach($form_state['values']['field_center_video'] as $video) {
if (!empty($video['value'])) {
$count++;
}
}
global $user;
// Only allow to upload 2 videos for role = staff
if (in_array('staff', $user->roles)) {
if ($count > 2) {
form_set_error('field_center_video', t('You cannot upload more than 2 videos'));
}
}
}
Source: DRUPAL – LIMIT THE NUMBER OF VALUES OF A CONTENT TYPE FIELD BY ROLE @ 2