Skip to content

Single attachment with title does work #13792

@ahukkanen

Description

@ahukkanen

Describe the bug

Given the following code for an attachment input breaks the attachment functionality (significant options are the first two options provided to the form builder):

    <%= form.attachment :images,
      multiple: false, # single file
      titled: true, # with a title
      label: t(".add_image"),
      button_label: t(".add_image_button"),
      button_edit_label: t(".add_image_edit"),
      help_i18n_scope: "decidim.forms.file_help.image",
      extension_allowlist: Decidim::OrganizationSettings.for(current_organization).upload_allowed_file_extensions_image %>

The goal here is to have a single attachment with the possibility to define its title in the upload modal.

This breaks because the upload modal assumes that all files with titles are defined in the multiple attachments mode. The significant lines that cause this issue in the JS are the following:

: `${modal.options.resourceName}[${modal.options.addAttribute}]`

: `${modal.options.resourceName}[${modal.options.addAttribute}]`

But with the title input, the input name is always defined assuming multiple files:

const titleField = `${modal.options.resourceName}[${modal.options.addAttribute}][${ix}][title]`

Now, the POST request payload would look something like this:

idea[add_images]: SIGNED_ID_FOR_THE_BLOB
idea[add_images][0][title]: image.jpg

And this breaks the internal logic in Rails causing the following exception:

ActionController::BadRequest (Invalid request parameters: expected Hash (got String) for param `add_images'):
  
rack (2.2.10) lib/rack/query_parser.rb:121:in `normalize_params'
rack (2.2.10) lib/rack/query_parser.rb:122:in `normalize_params'
rack (2.2.10) lib/rack/multipart/parser.rb:209:in `block (2 levels) in result'
rack (2.2.10) lib/rack/multipart/parser.rb:107:in `get_data'
rack (2.2.10) lib/rack/multipart/parser.rb:207:in `block in result'
rack (2.2.10) lib/rack/multipart/parser.rb:130:in `block in each'
rack (2.2.10) lib/rack/multipart/parser.rb:130:in `each'
rack (2.2.10) lib/rack/multipart/parser.rb:130:in `each'
rack (2.2.10) lib/rack/multipart/parser.rb:206:in `result'
rack (2.2.10) lib/rack/multipart/parser.rb:84:in `parse'
rack (2.2.10) lib/rack/multipart.rb:53:in `extract_multipart'
rack (2.2.10) lib/rack/request.rb:594:in `parse_multipart'
rack (2.2.10) lib/rack/request.rb:446:in `POST'
actionpack (6.1.7.10) lib/action_dispatch/http/request.rb:400:in `block (2 levels) in POST'
actionpack (6.1.7.10) lib/action_dispatch/http/parameters.rb:90:in `block in parse_formatted_parameters'
actionpack (6.1.7.10) lib/action_dispatch/http/parameters.rb:90:in `fetch'
actionpack (6.1.7.10) lib/action_dispatch/http/parameters.rb:90:in `parse_formatted_parameters'
actionpack (6.1.7.10) lib/action_dispatch/http/request.rb:399:in `block in POST'
rack (2.2.10) lib/rack/request.rb:69:in `fetch'
rack (2.2.10) lib/rack/request.rb:69:in `fetch_header'
actionpack (6.1.7.10) lib/action_dispatch/http/request.rb:398:in `POST'
actionpack (6.1.7.10) lib/action_dispatch/http/parameters.rb:55:in `parameters'
decidim-participatory_processes (0.28.4) app/constraints/decidim/participatory_processes/current_participatory_process.rb:22:in `matches?'

To Reproduce

  1. Look for one attachment field from the code, e.g. for proposals:
  2. Change the options passed to the form builder as multiple: false, titled: true
  3. Try to attach a file
  4. See the exception

Expected behavior

I would expect that a single file/attachment with a title can be defined as the title field is defined there for a reason (being able to control the alternative text for images, needed for accessibility).

Screenshots

No response

Stacktrace

No response

Extra data

  • Device: (any)
  • Device OS: (any)
  • Browser: (any)
  • Decidim Version: 0.30.0.dev
  • Decidim installation: local

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions