验证该值是否在一定范围内,例如1< = VAL< = 2 [英] Validate that a value is in a certain range, e.g. 1 <= val <=2

查看:124
本文介绍了验证该值是否在一定范围内,例如1< = VAL< = 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想验证数字:值将在1或2

 验证:值:格式=> {:带=> / 1 | 2 /,:消息=> 选择号码..}
 

然而,上述code失败验证时,价值== 1

请确保您的解决方案允许我补充一个消息的确认。

解决方案

 验证:价值:包括=> {:在=>并[1,2]}
 

请参阅<一href="http://apidock.com/rails/ActiveModel/Validations/HelperMethods/validates_inclusion_of">http://apidock.com/rails/ActiveModel/Validations/HelperMethods/validates_inclusion_of

I want to validate a number :value to be within 1 or 2

validates :value, :format => { :with => /1|2/, :message => "Select number.." }

However, the above code is failing the validation when value == 1

Please ensure that your solution allows me to add a message for the validation.

解决方案

validates :value, :inclusion => {:in => [1,2]}

See http://apidock.com/rails/ActiveModel/Validations/HelperMethods/validates_inclusion_of

这篇关于验证该值是否在一定范围内,例如1&LT; = VAL&LT; = 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆