Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #3699 (closed defect: fixed)

Opened 4 years ago

Last modified 4 years ago

[PATCH] validates_length_of counts wrong size when it's non-ascii strings.

Reported by: Masao Mutoh Assigned to: David
Priority: normal Milestone:
Component: ActiveRecord Version: 1.0.0
Severity: normal Keywords:
Cc:

Description

validates_length_of counts "byte" size not "character" size. So, if non-ascii strings are inputed, it doesn't work.

Attachments

activerecord-validates_length_of_utf8.patch (1.6 kB) - added by mutoh@highway.ne.jp on 02/02/06 14:20:22.
patch for validates_length_of .
activerecord-validates_length_of_utf8.patch2 (6.3 kB) - added by mutoh@highway.ne.jp on 02/02/06 15:53:02.
Applied comments.

Change History

02/02/06 14:20:22 changed by mutoh@highway.ne.jp

  • attachment activerecord-validates_length_of_utf8.patch added.

patch for validates_length_of .

02/02/06 14:52:29 changed by anonymous

1. This patch won't completely solve the issue. Code in active_record/validations.rb right above before patched lines also should be fixed:

case option         when :within, :in
          raise ArgumentError, ":#{option} must be a Range" unless option_value.is_a?(Range)

          too_short = options[:too_short] % option_value.begin
          too_long  = options[:too_long]  % option_value.end

          validates_each(attrs, options) do |record, attr, value|
            if value.nil? or value.size < option_value.begin
              record.errors.add(attr, too_short)
            elsif value.size > option_value.end
              record.errors.add(attr, too_long)
            end
          end

2. There's also in active_record/validations.rb method add_on_boundary_breaking, which also should be fixed.

3. validates_size_of (an alias of validates_length_of) is used in rails/activerecord/test/validations_test.rb: def test_validates_size_of_association , not sure how widespread and such usage is and if such overload is correct.

02/02/06 15:49:36 changed by Masao Mutoh

Thanks for comments.

I applied your comments and attachs new one.

And implemented testcase carefully.

Please check it again.

02/02/06 15:53:02 changed by mutoh@highway.ne.jp

  • attachment activerecord-validates_length_of_utf8.patch2 added.

Applied comments.

02/02/06 15:59:15 changed by Masao Mutoh

I don't know how to use add_on_boundary_breaking. So I didn't fix it. But others works correctly.

02/25/06 23:54:59 changed by david

  • status changed from new to closed.
  • resolution set to fixed.

(In [3654]) Fixed validates_length_of to work on UTF-8 strings by using characters instead of bytes (closes #3699) [Masao Mutoh]