version: Rails 4.1
5. 条件校验
有时当被给予的断言是满足条件的,才能去校验这个对象。你可以通过使用 :if 和 :unless 选项来实现,后面跟着的是一个标志,一个字符串,一段代码或者一个数组。当你想要执行校验时,你可以使用 :if 选项。当你想要不让校验发生时,那么你可以使用 :unless 选项。
5.1 Using a Symbol with :if
and :unless 使用一个标志通过 :if 和 :unless
你可以把 :if 和 :unless 选项关联到一个标志,来对应到一个方法名,然后可以在校验之前来调用该方法。这是该选项应用的最多的情况。
class Order < ActiveRecord::Base validates :card_number , presence: true , if : :paid_with_card ? def paid_with_card? payment_type == "card" end end |
5.2 Using a String with :if
and :unless 使用一个字符串通过 :if 和 :unless
你同样也能够使用一个字符串, 使用 eval 来执行校验, 其中需要包含有效的 Ruby 代码.
当字符串是一个真正的简短条件, 你应该只要使用这个选项.
class Person < ActiveRecord::Base
validates :surname , presence: true , if : "name.nil?"
end |
5.3 Using a Proc with :if
and :unless 使用一段代码, 通过 :if 和 :unless
最后, 可能通过 :if 和 :unless 后跟着一段被调用的代码对象. 使用一个代码对象来给予你编写内部条件的能力, 来替代一个单独的方法. 这个选项最好是用一行代码来解决.
class Account < ActiveRecord::Base
validates :password , confirmation: true ,
unless : Proc . new { |a| a.password.blank? }
end |
5.4 Grouping Conditional validations 分组条件的校验
有时候使用一个条件来进行多个校验是非常有用的, 使用 with_options 可以简单地来实现.
class User < ActiveRecord::Base with_options if : :is_admin ? do |admin| admin.validates :password , length: { minimum: 10 } admin.validates :email , presence: true end end |
在 with_options 代码块里所有的校验都会自动地传入条件 if: :is_admin?
5.5 Combining Validation Conditions 结合校验的条件
另一方面, 当多个条件定义是否让一个校验发生, 一个数组(Array)能够被用到. 你能够在同一个校验中同时调用 :if 和 :unless.
class Computer < ActiveRecord::Base validates :mouse , presence: true , if : [ "market.retail?" , :desktop ?] unless : Proc . new { |c| c.trackpad.present? } end |
当所有的 :if 条件为true 和没有 :unless条件为true的情况下, 校验才能执行.
相关推荐
验证方式流星变量验证的基础安装meteor add zhaoyao... * @param {array} params - a validation can have params if it needs * @param {array} validations - a validation can have sub validations if it needs */
Zipscene灰烬验证 Ember Validation的一个分支,它修复了非Ember-CLI安装中不支持的某些问题。 这些更改似乎是在Ember CLI版本中实现的,因此,只要有必要,就可以将其删除。
spring注解-validation所用到的jar包
A lib to make validations on java objects How add it Add the repository to your build.gradle with: repositories { maven { url "https://jitpack.io" } } and on your project dependencies: dependencies {...
The library uses the Validation Application Block (VAB) included in the MS Enterprise Library 4.1 to perform server-side validations for entities annotated with validation attributes. On the client-...
ember-i18n-cp-validations 在ember-cp-validations中增加了对ember-i18n的支持 使用ember-intl? 请参阅: 安装 ember install ember-i18n-cp-validations 在ember-i18n-cp-validations中在2.x和3.x之间进行重大...
5.20.14 ! There could be an AV when using multiple instances of the component. 5.20.13 + It's now possible to write unusual URL:s, like "oink://SaysTheSow". The condition is: a string of ascii chars ...
与 node-restify-validations 集成的 swagger-tools 的中间件扩展 安装 npm install swagger-restify-validation --save 用法 需要 swagger-restify 验证, var restifyValidation = require('swagger-restify-...
@ ember-intl / cp-validations 在ember-cp-validations中增加了对支持 使用ember-i18n? 请参阅: 要求 > = 2.0.0-rc.5 ember-cp-validations > = 2 安装 ember install @ember-intl/cp-validations 配置中 在...
是一个很棒的类似库Ember-validations 是一个 Ember.js 库,可以处理对象验证。 如果您必须检查对象属性的有效性,这个库会为您完成。 您只需要声明要验证的属性,以及要对该属性进行哪种验证。 这个库的灵感来自于...
源码AwesomeValidation,只需三步便可实现Android Validation校验功能,使得开发者重点关注他们的代码,校验的事情交给AwesomeValidation类库。它可以使得Android应用的布局代码更加整洁。可以校验数字类型,日期,...
This library is best used with Kotlin, and is to help reduce boilerplate code when writing validation rules for EditText fields. To install: Add Jitpack to your repositories in your build.gradle file ...
Laravel开发-laravel-validations 通用Laravel验证规则
Laravel开发-laravel-extended-validations 用于扩展默认Laravel验证的Laravel包
odin-form_validations 使用参数使用HTML5以及通过Validation / masked插件和Jquery对表单实施验证
产品特点PresenceValidator =>必填模型class User include ActiveModel :: Validations validates_presence_of :nameend 视图<%= f.text_field :name %> 其他text_field ish助手, text_area , radio_button...
ClientSideValidations使您的Rails 5.x / Rails 6.x应用程序变得容易! 项目目标 遵循开发的客户端验证最佳实践 自动提取服务器上定义的验证规则并将其应用于客户端。 在服务器端验证规则无法在客户端上运行的情况...
python库。 资源全名:validations-libs-1.0.4.tar.gz
python库。 资源全名:db_validations-0.1.1.tar.gz
Angular-ionic-forms-and-validations.zip,使用这个Ionic示例应用程序学习如何处理Ionic 3和Angular 4中的表单和验证。在这个精彩的教程中,我们将介绍简单的和自定义的验证,并教您如何处理错误消息。,Angularjs于...