[dm-validations] How to disable column name output into validation errors?

7 views
Skip to first unread message

Dan Sosedov

unread,
Jun 16, 2010, 8:40:44 PM6/16/10
to DataMapper
Hey,

I tried to find the way to disable/enable column (aka property name)
output into validation errors.
Lets say i have 2 situations:

1. Validate model, and show errors near each field unless model is
valid.
2. Validate mode, and show list of errors on top of page unless model
is valid.

Second one is easy, all you have to do - select @model.errors and do
whatever you want. But not sure about first method.

Is there anything like this:

class User
include DataMapper::Resource

property :id, Serial
property :name, String, :length => 2..32, :required => true
property :email, String, :format => :email_address, :required =>
true, :unique => true
end

And check validation with following statement (something im looking
for)

user = User.new(params)
unless user.valid?
errors = user.errors(:keep_names => false)
end

So, in errors output we`ll see only property name (symbol).

Any ideas?

Dan Sosedov

unread,
Jun 16, 2010, 8:47:27 PM6/16/10
to DataMapper
Oh, and i forgot to add, this can be done with custom validation
messages.

But the question still the same: is it possible to make this
conversions automatically?

Ripta Pasay

unread,
Jun 21, 2010, 10:13:01 AM6/21/10
to DataMapper
AFAIK, there isn't an option to disable column names from being
included.

Instead, I do what you describe: change the default validation
messages
(DataMapper::Validations::ValidationErrors.default_error_messages=).
Unfortunately, this also means I had to monkey-patch
DM::V::VE.default_error_message so that it doesn't interpolate the
field name.

-Ripta

Dan Sosedov

unread,
Jun 21, 2010, 6:18:53 PM6/21/10
to DataMapper
Yeah, i saw the default messages, but i dont really want to change
original gem.

Also, i noticed that custom validation errors does not work.

Example:

class User
include DataMapper::Resource

property :id, Serial
property :name, String
property :email, String, :length => 6..64, :format
=> :email_address
end

u = User.new(:name => 'My name', :email => 'te...@test.com')
if u.valid?
u.errors.add(:name, 'Invalid name')
puts "Valid: #{u.valid?}"
end

Or maybe im just doing it wrong. Ideas?

Ripta Pasay

unread,
Jun 21, 2010, 7:20:18 PM6/21/10
to datam...@googlegroups.com

Ah!

I just overrode DM::V::ValidationErrors.default_error_messages
somewhere after the gem is loaded, but before models are. In Rails 3,
this means creating an initializer and placing http://gist.github.com/447681
in it. Total monkey patch, but it works.

As for custom validation, you'd most likely want to create a validator
class that inherits from DM::V::GenericValidator. The catch with:

> if u.valid?
> u.errors.add(:name, 'Invalid name')
> puts "Valid: #{u.valid?}"
> end

is that calling #valid? actually clears all errors, then reruns all
validators for that resource.

However, if you only want to override the default validation error
messages, might I suggest another ugly hack: http://gist.github.com/447683
.


-rp

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

Reply all
Reply to author
Forward
0 new messages