3

I want to add an add more button to an element in Drupal form (using Drupal Form API). Should I make it with javascript (append a new element) and check for $_POST in submission or is there another solution in Drupal API.

This is my code for that field, I want users to be able to upload more than one image:

$form['product'] = array(
  '#title' => t('product'),
  '#type' => 'managed_file',
  '#required' => TRUE,
  '#default_value' => variable_get('product', ''),
  '#upload_location' => 'public://',
  '#upload_validators' => array(
    'file_validate_extensions' => array('gif png jpg jpeg'),
    'file_validate_size' => array(0.6*1024*1024),
  ),
);
  • Depending on what you're trying to do, there are several options. By "add more" do you mean to allow the user to add another value to a field (for example, to upload more than one file per Webform submission)? – beth Jun 25 '12 at 15:59
  • It looks like you want to use "Number of value:" option. it comes by default and you can find it in the configuration page of your field. – Tyler Durden Jun 25 '12 at 16:10
  • @beth as you said to upload more than file using form api – Ahmed Jun 25 '12 at 18:15
  • @SydBarrett I want to make a form using form Api not a cck – Ahmed Jun 25 '12 at 18:16
3

Use Drupal's AJAX Form functionality to add elements to a form from the client.

If you try to update a Drupal form with JavaScript on the client side, it will not be submitted correctly. Drupal knows what was in the form when it was sent to the client, and expects that same structure to be sent back. Therefore, you need to use AJAX to tell Drupal the form is updated (with more fields, etc.).

|improve this answer|||||
0

You can allow multiple file uploads per field with the multiupload filefield widget module and if you need image support, extend it with the multiupload imagefield widget as well.

|improve this answer|||||
0

For your specific need to do multiple uploading of images another option would be to use the media gallery module (which requires media module).

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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