Reputation: 4232
I'm trying to validate different input values. For example "domain" and "email". Both have to have a value and must be unique. So i try to validate them with
validates :domain,
:presence => true,
:uniqueness => { :case_sensitive => true }
validates :email,
:presence => true,
:uniqueness => { :case_sensitive => true }
But when i display the flash messages, i get four errors:
["Domain can't be blank", "Domain has already been taken", "Email can't be blank", "Email has already been taken"]
Is it possible to check them gradually? If the input field has no value, the user gets
["Domain can't be blank"]
but if the input field has a value and isn't unique, the user gets
["Domain has already been taken"]
How can i implement it?
Edit
Heres the code that prints the error messages:
<% [:error].each do |key| %>
<% if flash[key] %>
<div class="<%= key %>" id="flash">
<%= flash[key] %>
</div>
<% end %>
<% end %>
And here the controller that creates the errors:
def create
respond_to do |format|
# save form data
@login = Login.new(params[:login])
# if validation fails, throw error messages
if [email protected]
flash[:error] = @login.errors.to_a
end
# redirect to landingpage
format.html { redirect_to :root }
end
end
Upvotes: 0
Views: 1799
Reputation: 6153
flash[:error]
will display all errors if you don't first call @user.valid?
or @user.invalid?
which will then return the errors that pertain to that @person
object.
See the Rails guides on how to use the Flash. Also the validation errors section.
Give this a try:
if [email protected]
flash[:error] = @login.errors.to_a if @login.invalid?
end
Upvotes: 2