How to add a message to validates in model?












0















I have a form with a text field which needs to be a number and 5 or above.



Issue: I can't seem to get a message to appear when it's not validated



I have tried:



validates :price, :numericality => {:only_integer => true, greater_than_or_equal_to: 5, message: "should happen once per year" }


And this (I know this isn't a min number but just testing the message feature I saw on another post ... still no message if i use non-integer charecters.



 validates :price, format: { with: /Ad+z/, message: "Integer only. No sign allowed." }


The main layout has:



<% flash.each do |name, msg| %>
<% if msg.is_a?(String) %>
<div class="alert alert-<%= name.to_s == 'notice' ? 'success' : 'danger' %> alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
</div>
<% end %>
<% end %>


How can I have it so when the field entry isn't valid, it has a message appear?



SOLVED: kinda...



Just needed to add:



<% if @listing_video.errors.any? %>
<ul>
<% @listing_video.errors.full_messages.each do |msg| %>
<%= msg %>
<% end %>
</ul>
<% end %>


To my form.



It doesn't pop up like a banner atm but will figure that out soon with bootstrap and/or css



I usually remove this from my forms to avoid the "double" error message situation but this is a case where it is needed. I should had realized this before asking -










share|improve this question

























  • Welcome to stackoverflow. If you have solved your own problem you can answer your own question. Don't do this by editing the question as it breaks the Q&A format of the site.

    – max
    Jan 1 at 13:22


















0















I have a form with a text field which needs to be a number and 5 or above.



Issue: I can't seem to get a message to appear when it's not validated



I have tried:



validates :price, :numericality => {:only_integer => true, greater_than_or_equal_to: 5, message: "should happen once per year" }


And this (I know this isn't a min number but just testing the message feature I saw on another post ... still no message if i use non-integer charecters.



 validates :price, format: { with: /Ad+z/, message: "Integer only. No sign allowed." }


The main layout has:



<% flash.each do |name, msg| %>
<% if msg.is_a?(String) %>
<div class="alert alert-<%= name.to_s == 'notice' ? 'success' : 'danger' %> alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
</div>
<% end %>
<% end %>


How can I have it so when the field entry isn't valid, it has a message appear?



SOLVED: kinda...



Just needed to add:



<% if @listing_video.errors.any? %>
<ul>
<% @listing_video.errors.full_messages.each do |msg| %>
<%= msg %>
<% end %>
</ul>
<% end %>


To my form.



It doesn't pop up like a banner atm but will figure that out soon with bootstrap and/or css



I usually remove this from my forms to avoid the "double" error message situation but this is a case where it is needed. I should had realized this before asking -










share|improve this question

























  • Welcome to stackoverflow. If you have solved your own problem you can answer your own question. Don't do this by editing the question as it breaks the Q&A format of the site.

    – max
    Jan 1 at 13:22
















0












0








0








I have a form with a text field which needs to be a number and 5 or above.



Issue: I can't seem to get a message to appear when it's not validated



I have tried:



validates :price, :numericality => {:only_integer => true, greater_than_or_equal_to: 5, message: "should happen once per year" }


And this (I know this isn't a min number but just testing the message feature I saw on another post ... still no message if i use non-integer charecters.



 validates :price, format: { with: /Ad+z/, message: "Integer only. No sign allowed." }


The main layout has:



<% flash.each do |name, msg| %>
<% if msg.is_a?(String) %>
<div class="alert alert-<%= name.to_s == 'notice' ? 'success' : 'danger' %> alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
</div>
<% end %>
<% end %>


How can I have it so when the field entry isn't valid, it has a message appear?



SOLVED: kinda...



Just needed to add:



<% if @listing_video.errors.any? %>
<ul>
<% @listing_video.errors.full_messages.each do |msg| %>
<%= msg %>
<% end %>
</ul>
<% end %>


To my form.



It doesn't pop up like a banner atm but will figure that out soon with bootstrap and/or css



I usually remove this from my forms to avoid the "double" error message situation but this is a case where it is needed. I should had realized this before asking -










share|improve this question
















I have a form with a text field which needs to be a number and 5 or above.



Issue: I can't seem to get a message to appear when it's not validated



I have tried:



validates :price, :numericality => {:only_integer => true, greater_than_or_equal_to: 5, message: "should happen once per year" }


And this (I know this isn't a min number but just testing the message feature I saw on another post ... still no message if i use non-integer charecters.



 validates :price, format: { with: /Ad+z/, message: "Integer only. No sign allowed." }


The main layout has:



<% flash.each do |name, msg| %>
<% if msg.is_a?(String) %>
<div class="alert alert-<%= name.to_s == 'notice' ? 'success' : 'danger' %> alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
<%= content_tag :div, msg, :id => "flash_#{name}" %>
</div>
<% end %>
<% end %>


How can I have it so when the field entry isn't valid, it has a message appear?



SOLVED: kinda...



Just needed to add:



<% if @listing_video.errors.any? %>
<ul>
<% @listing_video.errors.full_messages.each do |msg| %>
<%= msg %>
<% end %>
</ul>
<% end %>


To my form.



It doesn't pop up like a banner atm but will figure that out soon with bootstrap and/or css



I usually remove this from my forms to avoid the "double" error message situation but this is a case where it is needed. I should had realized this before asking -







ruby-on-rails ruby






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 8:14







uno

















asked Dec 31 '18 at 7:08









unouno

15219




15219













  • Welcome to stackoverflow. If you have solved your own problem you can answer your own question. Don't do this by editing the question as it breaks the Q&A format of the site.

    – max
    Jan 1 at 13:22





















  • Welcome to stackoverflow. If you have solved your own problem you can answer your own question. Don't do this by editing the question as it breaks the Q&A format of the site.

    – max
    Jan 1 at 13:22



















Welcome to stackoverflow. If you have solved your own problem you can answer your own question. Don't do this by editing the question as it breaks the Q&A format of the site.

– max
Jan 1 at 13:22







Welcome to stackoverflow. If you have solved your own problem you can answer your own question. Don't do this by editing the question as it breaks the Q&A format of the site.

– max
Jan 1 at 13:22














2 Answers
2






active

oldest

votes


















0














This might help. A simple check for an amount



validate :amount_valid?

def amount_valid?
errors.add(:amount, "cant be zero") if self.amount.zero?
errors.add(:amount, "cant be negative") if self.amount.negative?
end


display errors on UI after validations if errors present :)






share|improve this answer































    0














    SOLVED:



    Just needed to add:



    <% if @listing_video.errors.any? %>
    <ul>
    <% @listing_video.errors.full_messages.each do |msg| %>
    <%= msg %>
    <% end %>
    </ul>
    <% end %>


    To my form.



    This creates an error for the validates above the input text field - which is what i want.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53984606%2fhow-to-add-a-message-to-validates-in-model%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      This might help. A simple check for an amount



      validate :amount_valid?

      def amount_valid?
      errors.add(:amount, "cant be zero") if self.amount.zero?
      errors.add(:amount, "cant be negative") if self.amount.negative?
      end


      display errors on UI after validations if errors present :)






      share|improve this answer




























        0














        This might help. A simple check for an amount



        validate :amount_valid?

        def amount_valid?
        errors.add(:amount, "cant be zero") if self.amount.zero?
        errors.add(:amount, "cant be negative") if self.amount.negative?
        end


        display errors on UI after validations if errors present :)






        share|improve this answer


























          0












          0








          0







          This might help. A simple check for an amount



          validate :amount_valid?

          def amount_valid?
          errors.add(:amount, "cant be zero") if self.amount.zero?
          errors.add(:amount, "cant be negative") if self.amount.negative?
          end


          display errors on UI after validations if errors present :)






          share|improve this answer













          This might help. A simple check for an amount



          validate :amount_valid?

          def amount_valid?
          errors.add(:amount, "cant be zero") if self.amount.zero?
          errors.add(:amount, "cant be negative") if self.amount.negative?
          end


          display errors on UI after validations if errors present :)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 31 '18 at 7:16









          Harsimar SandhuHarsimar Sandhu

          315




          315

























              0














              SOLVED:



              Just needed to add:



              <% if @listing_video.errors.any? %>
              <ul>
              <% @listing_video.errors.full_messages.each do |msg| %>
              <%= msg %>
              <% end %>
              </ul>
              <% end %>


              To my form.



              This creates an error for the validates above the input text field - which is what i want.






              share|improve this answer




























                0














                SOLVED:



                Just needed to add:



                <% if @listing_video.errors.any? %>
                <ul>
                <% @listing_video.errors.full_messages.each do |msg| %>
                <%= msg %>
                <% end %>
                </ul>
                <% end %>


                To my form.



                This creates an error for the validates above the input text field - which is what i want.






                share|improve this answer


























                  0












                  0








                  0







                  SOLVED:



                  Just needed to add:



                  <% if @listing_video.errors.any? %>
                  <ul>
                  <% @listing_video.errors.full_messages.each do |msg| %>
                  <%= msg %>
                  <% end %>
                  </ul>
                  <% end %>


                  To my form.



                  This creates an error for the validates above the input text field - which is what i want.






                  share|improve this answer













                  SOLVED:



                  Just needed to add:



                  <% if @listing_video.errors.any? %>
                  <ul>
                  <% @listing_video.errors.full_messages.each do |msg| %>
                  <%= msg %>
                  <% end %>
                  </ul>
                  <% end %>


                  To my form.



                  This creates an error for the validates above the input text field - which is what i want.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 1 at 22:34









                  unouno

                  15219




                  15219






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53984606%2fhow-to-add-a-message-to-validates-in-model%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Monofisismo

                      Angular Downloading a file using contenturl with Basic Authentication

                      Olmecas