0

Is it possible to show validation error messages from controller by making our method ? please check the code below

validate :validation
  def validation
    if self.RJan.nil? && self.RFeb.nil? && self.RMar.nil? && self.R1.nil?
      #How do write my error message here ?
    end
  end

and my form

<% if @record.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@record.errors.count, "error") %> prohibited this record from being saved:</h2>

      <ul>
      <% @record.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

1 Answer 1

1

You can add errors to your instance using

self.errors.add(:base, "your message here")

You could put attributes name as symbol instead of :base or whatever you like.

In your case

if self.RJan.nil? && self.RFeb.nil? && self.RMar.nil? && self.R1.nil?
  self.errors.add(:base, "your message here")
end
Sign up to request clarification or add additional context in comments.

3 Comments

so in the model in the if block i should write self.errors.full_messages ?
apparently something is wrong and it is not showing error message for any validation!
I answered your other thread. The code above is just to add new errors to your instance. Nothing to do with views or display mechanisms

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.