Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rendering integer field as string uses incorrect maxlength #1377

Open
timwis opened this issue Jan 29, 2024 · 1 comment
Open

Rendering integer field as string uses incorrect maxlength #1377

timwis opened this issue Jan 29, 2024 · 1 comment

Comments

@timwis
Copy link

timwis commented Jan 29, 2024

Hi team 馃憢馃徎 I believe I've encountered a bit of a bug.

I have an integer column in my database. Normally Formtastic would render this as <input type="number">, but I don't want it to be rendered as type="number" because the increment/decrement buttons that the browser puts on those inputs don't make sense for this column (in my case, it's a third party ID value, and thus would never be incremented/decremented, but this would apply in many scenarios).

So I render it as: :string. This uses a regular <input type="text"> but then Formtastic puts maxlength="3" on the column.

I believe this is because it [looks at the column's limit' property](https://github.com/formtastic/formtastic/blob/master/lib/formtastic/inputs/base/validations.rb#L185-L187). This makes sense for string columns - limitrefers to the limit of the string length. But forintegercolumns, thelimit` property refers to the size in bytes.

I think the fix should be to add a guard to that column_limit method that checks if column.type == :string or something, or perhaps do it in the method that calls column_limit. But I thought I'd check first whether I've misunderstood something.

In the meantime, my workaround is to add input_html: {maxlength: nil} to my input, to override this default functionality, but of course that's not ideal.

Thanks for your time.

@tcrane20
Copy link

tcrane20 commented Apr 9, 2024

Reporting the same issue, though we use PostgreSQL and the column is an int4 array (a list of appointment IDs). We use ActiveAdmin which uses Formtastic. By default, the array gets represented as an integer input field, so we created partials that set the input as a string

= f.input :appointment_ids, as: :string, #...redacted the rest

This wasn't an issue in the past for us until we upgraded from Rails 5.1 to 5.2 earlier this year (yeah, we're pretty far behind...). Turns out that ActiveRecord made an update because limits were not being set correctly for the PostgreSQL adapter: rails/rails@1a2fe7e

So now my int4[] went from returning a nil limit to 4.

I had to make this patch in our project:

module Formtastic
  module Inputs
    module Base
      module Stringish
        alias original_maxlength maxlength
        def maxlength
          if column? && column.try(:array?)
            options[:input_html].try(:[], :maxlength)
          else
            original_maxlength
          end
        end
      end
    end
  end
end

There's likely a better solution out there but I'm not gonna hold my breath. #1327 is related and hasn't seen any activity in 3 years.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants