One way to have full control over messages is to use your own validate block in the model. for example, to verify that the field is not empty, it will be as follows:
class MyModel < ActiveRecord::Base validate do |model| model.errors.add_to_base("My Custom message") if user.field.blank? end end
add_to_base designed to add messages that are not associated with a particular individual field (for example, if a combination of several fields is illegal). This means that CSS will not be added to highlight your invalid field. You can work around this by also adding a nil message to errors for your field, for example.
model.errors.add(:field, nil)
Alternatively, go to the custom-err-message plugin - this plugin allows you to not have your validation error message prefixed with the attribute name.
Update:
add_to_base deprecated with Rails 3. Instead, you can use the following: model_instance.errors.add(:base, "Msg")
Link: https://apidock.com/rails/ActiveRecord/Errors/add_to_base
mikej
source share