Sha256: 41112952ac8b5ecee727b69c62dfd7e22c5f903e8c0f0e2a20d23bf115f1f4a9

Contents?: true

Size: 1.65 KB

Versions: 25

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module Decidim
  module Attributes
    # Custom Virtus value to "standardize" the newline characters within strings
    # that are sent through user entered forms. This strips out the carriage
    # return characters from the strings in order to avoid validation mismatches
    # with the string lengths between the frontend and the backend.
    #
    # This type should be used with forms that have:
    # - A user input defined with the <textarea> element
    # - The input element having the `maxlength` attribute defined for it
    # - The backend having a maximum length validation for the input
    class CleanString < Virtus::Attribute
      # When using Windows or copying texts from existing documents, the text
      # can contain the carriage return characters (\r) that the front-end
      # character counter does not consider as actual characters. This happens
      # because the character counter currently counts the characters using
      # jQuery's `$input.val()` method which strips out the carriage return
      # characters as explained at:
      #   https://api.jquery.com/val/
      #
      #   Note: At present, using .val() on <textarea> elements strips carriage
      #   return characters from the browser-reported value.
      #
      # The backend, on the other hand, calculates these as characters as they
      # are included in the data that gets sent to the server. In order to fix
      # this mismatch, remove the carriage return characters from the text
      # using this attribute type.
      def coerce(value)
        return value unless value.is_a?(String)

        value.gsub(/\r/, "")
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
decidim-core-0.26.10 lib/decidim/attributes/clean_string.rb
decidim-core-0.26.9 lib/decidim/attributes/clean_string.rb
decidim-core-0.26.8 lib/decidim/attributes/clean_string.rb
decidim-core-0.26.7 lib/decidim/attributes/clean_string.rb
decidim-core-0.26.5 lib/decidim/attributes/clean_string.rb
decidim-core-0.26.4 lib/decidim/attributes/clean_string.rb
decidim-core-0.26.3 lib/decidim/attributes/clean_string.rb
decidim-core-0.26.2 lib/decidim/attributes/clean_string.rb
decidim-core-0.26.1 lib/decidim/attributes/clean_string.rb
decidim-core-0.26.0 lib/decidim/attributes/clean_string.rb
decidim-core-0.26.0.rc2 lib/decidim/attributes/clean_string.rb
decidim-core-0.26.0.rc1 lib/decidim/attributes/clean_string.rb
decidim-core-0.25.2 lib/decidim/attributes/clean_string.rb
decidim-core-0.25.1 lib/decidim/attributes/clean_string.rb
decidim-core-0.25.0 lib/decidim/attributes/clean_string.rb
decidim-core-0.25.0.rc4 lib/decidim/attributes/clean_string.rb
decidim-core-0.25.0.rc3 lib/decidim/attributes/clean_string.rb
decidim-core-0.25.0.rc2 lib/decidim/attributes/clean_string.rb
decidim-core-0.25.0.rc1 lib/decidim/attributes/clean_string.rb
decidim-core-0.24.3 lib/decidim/attributes/clean_string.rb