如何验证一个提交的ActiveRecord,而不是它的所有属性的单一属性? [英] How to validate a single attribute for a submitting ActiveRecord instead of all its attributes?
问题描述
我在使用Ruby on Rails 3,我想验证它的所有属性的一个提交ActiveRecord的一个属性,而不是。
I am using Ruby on Rails 3 and I would like to validate a single attribute for a submitting ActiveRecord instead of all its attributes.
例如,在我的模型我有:
For example, in my model I have:
validates :firstname, :presence => true, ...
validates :lastname, :presence => true, ...
我想的运行审定:姓
并在:名字
单独。 这可能吗?如果是这样,我怎么能作出这样的?
PS:我知道,为了进行验证有像validates_ presence_of,validates_confirmation_of的方法,... ...,但我想只使用上述code
P.S.: I know that for validation purposes there are methods like "validates_presence_of", "validates_confirmation_of", ..., but I would like to use only the above code.
推荐答案
您可以设置您的手机型号虚拟属性,然后做具体取决于属性的条件验证。
You can setup a virtual attribute on your model, and then do conditional validation depending on that attribute.
您可以找到关于这个截屏在 http://railscasts.com/episodes/41-有条件的验证
You can find a screencast about this at http://railscasts.com/episodes/41-conditional-validations
这篇关于如何验证一个提交的ActiveRecord,而不是它的所有属性的单一属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!