Rails 3 validates_format_of incorrectly validating as false for existing records

122 views
Skip to first unread message

joshmckin

unread,
Jan 7, 2011, 2:59:21 PM1/7/11
to SimpleRecord
Using SimpleRecord in a Rails3 project. Rails3 validates_format_of
works as expected for NEW records. However, on existing records
validates_format_of incorrectly fails validation even if the existing
value is valid. It likely has to do with values for attributes being
stored in arrays on lookups.


class User < SimpleRecord::Base
has_string :email

validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+
[a-z0-9]{2,4}$/i, :allow_blank => true
end

u = User.new
u.email = "go...@email.com"
u.valid? # => true
u.save

found = User.find(u.id)
found.email # => "go...@email.com
found.valid? # => false
found.errors # => {:email_address=>["is an invalid format."]}

found.email = "new...@email.com"
found.valid? # => true

Travis Reeder

unread,
Mar 15, 2011, 5:46:22 PM3/15/11
to simple...@googlegroups.com

I know this is old but if u get a chance to try it again, there were some big updates with regards to rails 3 / activemodel validations recently.

> --
> You received this message because you are subscribed to the Google Groups "SimpleRecord" group.
> To post to this group, send email to simple...@googlegroups.com.
> To unsubscribe from this group, send email to simple-recor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/simple-record?hl=en.
>

joshmckin

unread,
Mar 15, 2011, 6:56:07 PM3/15/11
to SimpleRecord
Well.... it works for SimpleRecord attributes but if I have an
attr_accessor it gives bogus errors

class Foo < SimpleRecord::Base
has_strings :password

attr_accessor :new_password

# For this example new_passwords must be formated like emails
validates_format_of :new_password, :with => /^[-a-z0-9_+\.]+\@([-a-
z0-9]+\.)+[a-z0-9]{2,4}$/i

# Stuff for encrypting and setting password...
end

bar = Foo.new
bar.new_password = te...@test.com
bar.valid? #=> false
bar.errors #=> {:new_password=>["is invalid"]}


Thanks,

Josh


On Mar 15, 4:46 pm, Travis Reeder <tree...@gmail.com> wrote:
> I know this is old but if u get a chance to try it again, there were some
> big updates with regards to rails 3 / activemodel validations recently.
> On Jan 7, 2011 11:59 AM, "joshmckin" <joshmc...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Using SimpleRecord in a Rails3 project. Rails3 validates_format_of
> > works as expected for NEW records. However, on existing records
> > validates_format_of incorrectly fails validation even if the existing
> > value is valid. It likely has to do with values for attributes being
> > stored in arrays on lookups.
>
> > class User < SimpleRecord::Base
> > has_string :email
>
> > validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+
> > [a-z0-9]{2,4}$/i, :allow_blank => true
> > end
>
> > u = User.new
> > u.email = "g...@email.com"
> > u.valid? # => true
> > u.save
>
> > found = User.find(u.id)
> > found.email # => "g...@email.com
> > found.valid? # => false
> > found.errors # => {:email_address=>["is an invalid format."]}
>
> > found.email = "newg...@email.com"

Travis Reeder

unread,
Mar 16, 2011, 5:48:43 PM3/16/11
to simple-record
Ahhh, got it. I'll have a look next week (out this week). Can you make an issue on github?
Reply all
Reply to author
Forward
0 new messages