validates

In addition to those validations, information is provided with each macro about its specific options.

Validation Macro Options

Validate acceptance of terms.

validates_acceptance_of :terms
validates :terms, acceptance: true
:message
:accept # Specify the accepted value. Default: 1.

Validate associated documents when the parent is validated. This only validates documents in memory.

validates_associated :albums
validates :albums, associated: true
:message

Validate confirmation of a field, with a "_confirmation" suffix.

validates_confirmation_of :password
validates :password, confirmation: true
:message

Validate items are not in a list.

validates_exclusion_of :employers, in: [ "SoundCloud" ]
validates :employers, exclusion: { in: [ "SoundCloud" ] }
:in # Required list of values.
:message

Validate the format of a field.

validates_format_of :title, with: /\A\w+\Z/
validates :title, format: { with: /\A\w+\Z/ }
:allow_blank # Whether the field can be blank.
:in # A list or range of values.
:message
:with # A regular expression to match.
:without # A regular expression not to match.

Validate items are included in a list.

validates_inclusion_of :employers, in: [ "SoundCloud" ]
validates :employers, inclusion: { in: [ "SoundCloud" ] }
:allow_blank # Whether the field can be blank.
:in # Required list of values.
:message

Validate the length of a field.

validates_length_of :password, minimum: 8, maximum: 16
validates :password, length: { minimum: 8, maximum: 16 }
:allow_blank # Whether to validate blank attributes.
:in # The range the length can fall within.
:maximum # The maximum length of the attribute.
:message
:minimum # The minimum length of the attribute.
:tokenizer # A block tokenizer.
:too_long # Custom message if too long.
:too_short # Custom message if too short.
:within # Range the length can fall within.
:wrong_length # Custom message for an incorrect length.

Validate the numericality of a field.

validates_numericality_of :age, even: true
validates :age, numericality: { even: true }
:equal_to # A value the field must be exactly.
:even # Set that the value must be even.
:greater_than
:greater_than_or_equal_to
:less_than
:less_than_or_equal_to
:message
:odd # Set that the value must be odd.
:only_integer # Set whether the value has to be an integer.

Validate that an attribute exists. Note that if you add a presence validation to a relation, then Mongoid will enable autosave for that relation.

validates_presence_of :name
validates :name, presence: true
:message

Validate that an attribute is unique. Note that for embedded documents, this will only check that the field is unique within the context of the parent document, not the entire database.

validates_uniqueness_of :name
validates :name, uniqueness: true
:message
:case_sensitive # Whether to use case sensitive matching.
:scope # Scope checks to the value of this field.

validates

时间: 2024-10-24 10:24:56

validates的相关文章

在特定的action里使用validates

http://guides.rubyonrails.org/v3.0.8/active_record_validations_callbacks.html#on The :on option lets you specify when the validation should happen. The default behavior for all the built-in validation helpers is to be run on save (both when you’re cr

[Schema]I have updated my XML Schema for my service but SoapUI still generates/validates according to the old schema.

SoapUI caches XML schemas when they are first loaded. If you need to force a reload of an interfaces schema either restart SoapUI or use the "Update Definition" action for an interface and just specify the same URL as the current one.

PHPActiveRecord validates

validates_presence_of #检测是不是为空 为空的话可以抛出异常 *Model类: static $validates_presence_of = array( array('title', 'message' => '不能为空') ); 实例化后: $user = new User(array('name'=>'')); $user->save(); var_dump($user->errors->on('name')); #=> 不能为空 vali

rails 常用的验证方法 validates (转)

Agile Web Development with Rails 17.4 validation validate              在save的时候激活validate_on_create      createvalidate_on_update      update 通过这三个方法可以添加validates_XXX_xxx没有的功能错误信息写进err#:name 是验证的属性名def validate    unless name && name =~ /^\w+$/  

App 组件化/模块化之路——Android 框架组件(Android Architecture Components)使用指南

面对越来越复杂的 App 需求,Google 官方发布了Android 框架组件库(Android Architecture Components ).为开发者更好的开发 App 提供了非常好的样本.这个框架里的组件是配合 Android 组件生命周期的,所以它能够很好的规避组件生命周期管理的问题.今天我们就来看看这个库的使用. 通用的框架准则 官方建议在架构 App 的时候遵循以下两个准则: 关注分离 其中早期开发 App 最常见的做法是在 Activity 或者 Fragment 中写了大量

Revit 2017 编程须要用Visual Studio2015 +.NET Framework 4.52

一年一度的Revit产品公布时刻,我们抢先想各位介绍下Revit 2017的变化和新功能 Major changes and renovations to the Revit API API changes .NET 4.6 All Revit API binaries are now built targeting .NET 4.5.2. However, Revit uses the runtime from .NET 4.6. At a minimum, add-ins will need

ruby reduce方法

Ruby 中一些好用的方法(注意reduce方法) 2016-07-27 17:57 370人阅读 评论(0) 收藏 举报 #####inject inject是我使用最频繁的方法了,它的强大之处在于可以方便的对嵌套的数组,哈希等混合数据结构进行合并或求和, 可以有效减少代码量. 例如最常见的数组套哈希: 1 2 3 4 array = [{a:100}, {b:200}, {c:300}] array.inject(0) { |sum, e| sum += e.values.first } #

Interview

下面的题是供大家查漏补缺用的,真正的把这些题搞懂了,才能"以不变应万变". 回答问题的时候能联系做过项目的例子是最好的,有的问题后面我已经补充联系到项目中的对应的案例了. 1.简述 private. protected. public. internal 修饰符的访问权限.  private : 私有成员, 在类的内部才可以访问. protected : 保护成员,该类内部和继承类中可以访问. public : 公共成员,完全公开,没有访问限制. internal: 当前程序集内可以访

REST Security with JWT using Java and Spring Security

Security Security is the enemy of convenience, and vice versa. This statement is true for any system, virtual or real, from the physical house entrance to web banking platforms. Engineers are constantly trying to find the right balance for the given