Remove model field name in rails validation

By default rails show field name before it’s default message.

For example default rails validation:

validates_xxx_of :the_column, :message => "Please add this!" will show message "The ColumnPlease add this!"

For project purposes we need some time to add custom error messages without a field name in rails validation.

We can solve this by adding custom method in model.

In rails 2: you can do on your model:

validate :test_validation

private

def test_validation
errors.add_to_base("Message")
end

In rails 3 or greater:

validate :test_validation

private

def test_validation
errors.add(:base, 'Message')
end
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s