0

I have text field with name print run which has to be validated in the format of

Valid values for the print run field, are positive integers, 'Unlimited', and 'Silent'.

I have added validation like this

VALID_NAMES = %w(Unlimited silent  #k)
  validates_inclusion_of :print_run, :in => VALID_NAMES

how to proceed for validation to accept positive integers also...

validates_numericality_of :print_run, :only_integer => true, :message => "can only be whole number."

the above validation accept only numeric

Integer(attributes_before_type_cast["print_run"])        
errors.add_to_base( "print_run must be a number")    

the above statement accepts only the number but no validation for positive integers

how to proceed with this..

Baldrick
  • 23,882
  • 6
  • 74
  • 79
yamuna
  • 21
  • 3

1 Answers1

0

if you want only positive integers,

validates_numericality_of :print_run, :only_integer => true,:greater_than_or_equal_to => 0, :message => "can only be whole number."

if you want Unlimited or silent or positive integers

validates_format_of :print_run, :with => /\A(Unlimited|silent|\d*)\z/, :message => "your custom message"
shweta
  • 8,019
  • 1
  • 40
  • 43