验证字符串的最小和最大长度,但允许它为空

时间:2021-12-23 02:58:30

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?

我如何写这篇文章来实现我的目标?

8 个解决方案

#1


105  

I think it might need something like:

我认为它可能需要这样的东西:

validates_length_of :foo, :minimum => 5, :maximum => 5, :allow_blank => true

More examples: ActiveRecord::Validations::ClassMethods

更多的例子:ActiveRecord::验证::类方法

#2


92  

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

#3


9  

From the validates_length_of documentation:

从validates_length_of文档:

validates_length_of :phone, :in => 7..32, :allow_blank => true

:allow_blank - Attribute may be blank; skip validation.

:allow_blank -属性可以为空;跳过验证。

#4


5  

Or even more concise (with the new hash syntax), from the validates documentation:

或者更简洁(使用新的哈希语法),来自validates文档:

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..””,但只是回答了这个问题。

#5


2  

every validates_* accepts :if or :unless options

每个validates_*接受:如果或:除非选项。

validates_length_of :foo, :maximum => 5, :if => :validate_foo_condition

where validate_foo_condition is method that returns true or false

validate_foo_condition在哪里返回真或假

you can also pass a Proc object:

您还可以传递一个Proc对象:

validates_length_of :foo, :maximum => 5, :unless => Proc.new {|object| object.foo.blank?}

#6


2  

How about that: validates_length_of :foo, is: 3, allow_blank: true

那么:validates_length_of:foo,是:3,allow_blank: true

#7


1  

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) }

#8


-4  

In your model e.g.

在你的模型。

def validate
  errors.add_to_base 'error message' unless self.foo.length == 5 or self.foo.blanc?
end

#1


105  

I think it might need something like:

我认为它可能需要这样的东西:

validates_length_of :foo, :minimum => 5, :maximum => 5, :allow_blank => true

More examples: ActiveRecord::Validations::ClassMethods

更多的例子:ActiveRecord::验证::类方法

#2


92  

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

#3


9  

From the validates_length_of documentation:

从validates_length_of文档:

validates_length_of :phone, :in => 7..32, :allow_blank => true

:allow_blank - Attribute may be blank; skip validation.

:allow_blank -属性可以为空;跳过验证。

#4


5  

Or even more concise (with the new hash syntax), from the validates documentation:

或者更简洁(使用新的哈希语法),来自validates文档:

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..””,但只是回答了这个问题。

#5


2  

every validates_* accepts :if or :unless options

每个validates_*接受:如果或:除非选项。

validates_length_of :foo, :maximum => 5, :if => :validate_foo_condition

where validate_foo_condition is method that returns true or false

validate_foo_condition在哪里返回真或假

you can also pass a Proc object:

您还可以传递一个Proc对象:

validates_length_of :foo, :maximum => 5, :unless => Proc.new {|object| object.foo.blank?}

#6


2  

How about that: validates_length_of :foo, is: 3, allow_blank: true

那么:validates_length_of:foo,是:3,allow_blank: true

#7


1  

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) }

#8


-4  

In your model e.g.

在你的模型。

def validate
  errors.add_to_base 'error message' unless self.foo.length == 5 or self.foo.blanc?
end