Ruby-on-rails Rails:验证字符串的最小和最大长度,但允许它为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4441882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails: Validating min and max length of a string but allowing it to be blank
提问by bgadoci
I have a field that I would like to validate. I want the field to be able to be left blank, but if a user is entering data I want it to be in a certain format. Currently I am using the below validations in the model, but this doesn't allow the user to leave it blank:
我有一个要验证的字段。我希望该字段能够留空,但如果用户正在输入数据,我希望它采用某种格式。目前我在模型中使用以下验证,但这不允许用户将其留空:
validates_length_of :foo, :maximum => 5
validates_length_of :foo, :minimum => 5
How do I write this to accomplish my goal?
我如何写这个来实现我的目标?
回答by DigitalRoss
I think it might need something like:
我认为它可能需要类似的东西:
validates_length_of :foo, minimum: 5, maximum: 5, allow_blank: true
More examples: ActiveRecord::Validations::ClassMethods
回答by quainjn
You can also use this format:
您也可以使用这种格式:
validates :foo, length: {minimum: 5, maximum: 5}, allow_blank: true
Or since your min and max are the same, the following will also work:
或者由于您的最小值和最大值相同,以下也将起作用:
validates :foo, length: {is: 5}, allow_blank: true
回答by Gareth
From the validates_length_of documentation:
validates_length_of :phone, :in => 7..32, :allow_blank => true
:allow_blank- Attribute may be blank; skip validation.
:allow_blank- 属性可以为空;跳过验证。
回答by Fellow Stranger
Or even more concise (with the new hash syntax), from the validates documentation:
或者更简洁(使用新的哈希语法),来自验证文档:
validates :foo, length: 5..5, allow_blank: true
The upper limit should probably represent somehting more meaningful like "in: 5..20", but just answering the question to the letter.
上限可能应该代表更有意义的东西,例如“in:5..20”,但只是回答问题。
回答by keymone
every validates_* accepts :if or :unless options
每个 validates_* 都接受 :if 或 :unless 选项
validates_length_of :foo, :maximum => 5, :if => :validate_foo_condition
where validate_foo_condition is method that returns true or false
其中 validate_foo_condition 是返回 true 或 false 的方法
you can also pass a Proc object:
你也可以传递一个 Proc 对象:
validates_length_of :foo, :maximum => 5, :unless => Proc.new {|object| object.foo.blank?}
回答by shiva kumar
validates_length_of :reason, minimum: 3, maximum: 30
rspec for the same is
相同的 rspec 是
it { should validate_length_of(:reason).is_at_least(3).is_at_most(30) }
回答by shem
How about that:
validates_length_of :foo, is: 3, allow_blank: true
那个怎么样:
validates_length_of :foo, is: 3, allow_blank: true
回答by Marlon Henry Schweigert
Add in your model:
在您的模型中添加:
validates :color, length: { is: 7 }
color is a string:
颜色是一个字符串:
t.string :color, null: false, default: '#0093FF', limit: 7
回答by codevoice
In your model e.g.
在您的模型中,例如
def validate
errors.add_to_base 'error message' unless self.foo.length == 5 or self.foo.blanc?
end

