This blog is part of our Rails 5 series.
Let’s look at a validation example in Rails 4.x.
In this case,
we do not get
about the type of failed validation
ActiveModel#Errors only gives
the attribute name and the translated error message.
This works out well for normal apps.
But in case of API only applications,
we want to allow
the client consuming the API
to generate customized error message as per their needs.
We don’t want to send the final translated messages in such cases.
if we could just send details
presence validation failed for
the client app would be able to customize the error message
based on that information.
In Rails 5, it is now possible to get such details about which validations failed for a given attribute.
We can check this by calling
We can also add custom validator types as per our need.
Passing contextual information about the errors
We can also send contextual data for the validation to the
This data can be later accessed
forwards all options except
For eg. we can say that the
password is invalid because
! is not allowed, as follows.
We can also use this feature in our Rails 4.x apps by simply installing gem active_model-errors_details.