Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7840201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:41:52+00:00 2026-06-02T15:41:52+00:00

I have a custom validator and I am trying to output an error message

  • 0

I have a custom validator and I am trying to output an error message when it fails but have been unable to do so. Could someone please tell me if I am doing this in the correct place.

class User < ActiveRecord::Base
  self.table_name = "user"

  attr_accessible :name, :ip, :printer_port, :scanner_port

  validates :name,        :presence => true,
                          :length => { :maximum => 75 },
                          :uniqueness => true                          

  validates :ip,          :length => { :maximum => 75 },
                          :allow_nil => true     

  validates :printer_port, :presence => true, :if => :has_association? 

  validates :scanner_port, :presence => true, :if => :has_association?          

  def has_association?
    ip != nil
  end
end

I had it as follows:

validates :printer_port, :presence => true, :message => "can't be blank", :if => :has_wfm_association?

But was receiving an error

Unknown validator: 'MessageValidator'

And when I tried to put the message at the end of the validator the comma seperating the has_association? turned the question mark and comma orange

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:41:55+00:00Added an answer on June 2, 2026 at 3:41 pm

    The message and if parameters should be inside a hash for presence:

    validates :printer_port, :presence => {:message => "can't be blank", :if => :has_wfm_association?}
    

    This is because you can load multiple validations in a single line:

    validates :foo, :presence => true, :uniqueness => true
    

    If you tried to add a message to that the way you did, or an if condition, Rails wouldn’t know what validation to apply the message/conditional to. So instead, you need to set the message per-validation:

    validates :foo, :presence => {:message => "must be present"},
                    :uniqueness => {:message => "must be unique"}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom password validator that someone gave me in an answer to
I am trying to make cutom validator controls but dont know why the Message
I'm trying to localize error messages from NHibernate.Validator. From what I have read, the
I'm trying to make a custom validator work on my app. I have already
i am trying to build an custom validator with symfony2 but something strange happens:
I'm trying to set up a custom message source for Hibernate Validator 4.1 through
I have a custom validator attribute which I am trying to unit test. In
I have been trying to use regex but I cant seem to get it
I have been creating a custom username/password validator for a WCF service and ran
I'm trying to setup custom validation for a checkbox. I have 7 checkboxes each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.