1

I have taken a field of type field collection in my user-profile-form and its cardinality is set to unlimited as I wanted to have "add more" button for the users. I want to restrict the addition of fieldset to five numbers.

If I change the number of fields to be displayed to 5 from edit, it renders all the five fieldsets altogether which I do not want.

I tried overriding field_multiple_value_form() in my template.php file but did not succeeded. Please suggest some ideas to achieve this. Thanks In advance.

2 Answers 2

1

This can be achieved using css. First of all write hook_form_alter() on your custom module. and your code should be like this..


function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'your_form_id') {
    if (isset($form['your_field']['und']['#max_delta']) && $form['your_field']['und']['#max_delta'] == 4) {
      $form['your_field']['und']['add_more']['#prefix'] = '';
      $form['your_field']['und']['add_more']['#suffix'] = '';    
    }
  }
}

Where $form['your_field'] is your field collection field machine name. You can write your custom css on #prefix and #suffix and set it to display none. you can set display none to wrapper div element

0
0

Try using this module Multivalue Settings

This module adds some useful settings for fields that can contain multiple values. You can specify:

Number of values allowed Number of values required Number of empty widgets to display initially (Drupal usually displays all fields for non-unlimited fields. This allows you to only display a few, followed by the 'Add More' button, which will disappear after the maximum number of fields has been filled.) Number of empty widgets to display on edit forms after the field contains some values (Drupal usually displays one extra)

I had tested and worked well for me. But please use this module at your own risk, as the module owner himself said that its quite buggy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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