I've got a simple active record validation on an object using this within a form:
form.error_messages({:message => '', :header_message => ''})
This in turn outputs something like "FieldName My Custom message"
What i need to do is remove the field name from the error message but leave my custom message.
Can anyone point me in the right direction for this.
In rails 3.2.6, you can set this in a locale file (e.g., config/locales/en.yml):
en:
errors:
format: "%{message}"
Otherwise, the default format is "%{attribute} %{message}".
One way to have complete control over the messages is to use a custom validate block in the model. e.g. to check that a field is not blank it would be like this:
class MyModel < ActiveRecord::Base
validate do |model|
model.errors.add_to_base("My Custom message") if user.field.blank?
end
end
add_to_base is intended for adding messages that aren't related to a particular individual field (e.g. if a combination of multiple fields is illegal). This means that the CSS to highlight your invalid field won't get added. You can work arround this by also adding a nil message to the errors for your field e.g.
model.errors.add(:field, nil)
Alternatively, check out custom-err-message plugin - this plugin gives you the option to not have your custom validation error message prefixed with the attribute name.
Update:
add_to_base is deprecated since Rails 3. The following can be used instead:
model_instance.errors.add(:base, "Msg")
Ref: https://apidock.com/rails/ActiveRecord/Errors/add_to_base
You can use errors.add_to_base http://api.rubyonrails.org/classes/ActiveRecord/Errors.html#M001712
You can access the object's errors instance directly if you need full control over how the messages are presented.
Related
I have an if statement in my update action in one of my controllers. It looks like this:
if !#bid.attributes.values.include?(nil)
build(#bid.id)
end
I am checking to see if there are any nil valued attributes in my Bid object before building a bid report. I know the build method works fine because it builds a report when not wrapped in the if statement. When it is wrapped in this if statement, it doesn't run. I have checked to make sure that there are no nil values in the object. I went into the rails console and all attributes have non-nil values. In addition, I am able to check this in the views to confirm that there are no nil values.
I have also tried writing as:
build(#bid.id) unless #bid.attributes.values.include?(nil)
and a couple other variations. None are allowing the build to run.
Your code seems fine, I'm betting it's your data that's the problem instead. Mostly likely, assuming this an active record instance, the attribute is id which will be nil until the new record gets saved.
What do you get in the terminal when you add this line right before your if?
puts #bid.attributes.to_yaml
You should be able to see what has values and what does not. And I'm pretty sure at least one of those values is nil.
I would recommend being more explicit about exactly which fields are required. And this is exactly what validations are for.
class Person < ActiveRecord::Base
validates :name, presence: true
end
You explicitly validate each field so that when it's absent you get a very specific error message about why: "Person name can't be blank." So instead of wondering why it wont save, you get told why at the point it fails to save.
Active Record validations throw an error when they fail. What I have in a model is
validate_format_of :field_which_cannot_have_spaces, :with => /^[^\s]+$/, :message => "Some error message"
What I want instead, is for a string replacement to substitute spaces for underscores (snake_case).
The advantages of using validation for me, are that it runs every time the field is changed unless save(validate: false), and that I don't need to repeat the replacement in the create and update controller methods.
Front end javascript solutions won't help if the user hacks the form... a rails solution is needed!
It sounds like you want a callback rather than a validation. This can run each time your object is modified.
So, to remove spaces from your field before the object is saved you can do:
before_save :remove_spaces_from_x
def remove_spaces_from_x
self.field_which_cannot_have_spaces.gsub!("\s","_")
end
Note also that validation do not always raise an error when they fail. If you use save! or create! then an error is raised but if you use the equivalent save or create then no error is raised, false is returned and the object's errors are populated with details of the validation failure.
Co-worker just told me to do the following in the model:
def field_which_cannot_have_spaces=(input_from_form)
super(input_from_form.gsub("\s","_"))
end
This will change the value as it is set.
"Validations are for informing the client there is a problem, and shouldn't be doing something other than throwing an error."
Hope this helps someone else...
If you need to code a considerably complex validation, the error sometimes doesnt lie in a particular attribute, but in a combination of several of them.
For example, if i want to validate that a the time period between :start_date and :end_date doesnt contain any sunday, the error doesnt belong specifically to either of those fields, but the Errors add method requires to specify it.
Try doing something like this:
# Your Model.rb
validate :my_own_validation_method
...
private
def my_own_validation_method
if there_is_no_sunday_in_the_range
self.errors[:base] << "You must have a Sunday in the time range!"
end
end
Basically, you can add your own complex validations to a model, and when you see that something erroneous has happened, you can add an error string in the array of errors.
model_instance.errors[:base] << "msg"
The answers above are outdated. For Rails 5 and higher you need to call the ActiveModel::Errors add method with :base as the first parameter. See the example below.
model_instance.errors.add(
:base,
:name_or_email_blank,
message: "either name or email must be present"
)
You can use errors[:base] to add general errors that aren't specifically tied to one attribute - rails guide link.
You can actually name the hash key whatever you'd like:
instance.errors[:case_of_the_sundays] << "Error, son."
Just a little more descriptive.
Update:
message array in order to add an error is deprecated.
Now we do something like that.
self.errors.add(:some_key, "Some Error!")
I know how to check an attribute for errors:
#post.errors[:title].any?
Is it possible to check which validation failed (for example "uniqueness")?
Recently I came across a situation where I need the same thing: The user can add/edit multiple records at once from a single form.
Since at validation time not all records have been written to the database I cannot use #David's solution. To make things even more complicated it is possible that the records already existing in the database can become duplicates, which are detected by the uniqueness validator.
TL;DR: You can't check for a specific validator, but you can check for a specific error.
I'm using this:
# The record has a duplicate value in `my_attribute`, detected by custom code.
if my_attribute_is_not_unique?
# Check if a previous uniqueness validator has already detected this:
unless #record.errors.added?(:my_attribute, :taken)
# No previous `:taken` error or at least a different text.
#record.errors.add(:my_attribute, :taken)
end
end
Some remarks:
It does work with I18n, but you have to provide the same interpolation parameters to added? as the previous validator did.
This doesn't work if the previous validator has written a custom message instead of the default one (:taken)
Regarding checking for uniqueness validation specifically, this didn't work for me:
#post.errors.added?(:title, :taken)
It seems the behaviour has changed so the value must also be passed. This works:
#post.errors.added?(:title, :taken, value: #post.title)
That's the one to use ^ but these also work:
#post.errors.details[:title].map { |e| e[:error] }.include? :taken
#post.errors.added?(:title, 'has already been taken')
Ref #34629, #34652
By "taken", I assume you mean that the title already exists in the database. I further assume that you have the following line in your Post model:
validates_uniqueness_of :title
Personally, I think that checking to see if the title is already taken by checking the validation errors is going to be fragile. #post.errors[:title] will return something like ["has already been taken"]. But what if you decide to change the error message or if you internationalize your application? I think you'd be better off writing a method to do the test:
class Post < ActiveRecord::Base
def title_unique?
Post.where(:title => self.title).count == 0
end
end
Then you can test if the title is unique with #post.title_unique?. I wouldn't be surprised if there's already a Rubygem that dynamically adds a method like this to ActiveRecord models.
If you're using Rails 5+ you can use errors.details. For earlier Rails versions, use the backport gem: https://github.com/cowbell/active_model-errors_details
is_duplicate_title = #post.errors.details[:title].any? do |detail|
detail[:error] == :uniqueness
end
Rails Guide: http://guides.rubyonrails.org/active_record_validations.html#working-with-validation-errors-errors-details
I am using the 'paperclip' gem for Ruby on Rails 3 and I would like to handle a custom presence validation with this approach: "if a file path value (a string like "/Users/Desktop/image.gif" or "C:/Users/Desktop/image.gif") is not entered in the 'file_field' throws an error for this field".
I think that I have a problem to do that because RoR handles differently 'file_field's than others fields.
Notice: I don't want use the 'validates_attachment_presence' method present in 'paperclip'.
Is it a good approach to do what I would like? If so, how to do that?
I tryed in my controller the code:
if params[:user][:avatar].blank?
# I also used
# - 'nil?' instead of 'blank'
# - !params[:user][:avatar]
#user.errors.add( :avatar, "can not be blank" )
end
but, if I try to submit the form with an empty file (a nil/blank path value), I get a
NoMethodError
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
Just an idea: you could also use Javascript to not enable the upload button until something is typed in the text field first. This avoids the need for unnecessary round trip to the server. You might want to validate that if your User.rb model is using attr_accessible, the field is present in that line.