当前位置: 首页 > news >正文

Ruby On Rails 笔记5——常用验证下

3.Validation Options

3.1 :allow_nil

当验证值为nil时:allow_nil选项会跳过验证

class Coffee < ApplicationRecordvalidates :size, inclusion: { in: %w(small medium large),message: "%{value} is not a valid size" }, allow_nil: true
end
irb> Coffee.create(size: nil).valid?
=> true
irb> Coffee.create(size: "mega").valid?
=> false

3.1 :allow_blank

如果属性值为blank?,则跳过验证。

class Topic < ApplicationRecordvalidates :title, length: { is: 5 }, allow_blank: true
end
irb> Topic.create(title: "").valid?
=> true
irb> Topic.create(title: nil).valid?
=> true
irb> Topic.create(title: "short").valid?
=> false # 'short' is not of length 5, so validation fails even though it's not blank

3.3 :message

允许指定验证失败时添加到errors集合中的消息,否则,会使用默认错误消息。

class Person < ApplicationRecord# Hard-coded messagevalidates :name, presence: { message: "must be given please" }# Message with dynamic attribute value. %{value} will be replaced# with the actual value of the attribute. %{attribute} and %{model}# are also available.validates :age, numericality: { message: "%{value} seems wrong" }
end

Proc :message值有两个参数:正在验证的对象以及包含:model、:attribute 和 :value 键值对的hash。

class Person < ApplicationRecordvalidates :username,uniqueness: {# object = person object being validated# data = { model: "Person", attribute: "Username", value: <username> }message: ->(object, data) do"Hey #{object.name}, #{data[:value]} is already taken."end}
end

3.4 :on

on 选项允许你指定验证发生时间。默认情况下设置的验证会在save时运行,可以根据需要修改

class Person < ApplicationRecord# it will be possible to update email with a duplicated valuevalidates :email, uniqueness: true, on: :create# it will be possible to create the record with a non-numerical agevalidates :age, numericality: true, on: :update# the default (validates on both create and update)validates :name, presence: true
end
4. Conditional Validations

有时,只有在满足给定条件时才能验证对象,这可以通过:if 和 :unless 选项来实现。

class Order < ApplicationRecordvalidates :card_number, presence: true, if: :paid_with_card?def paid_with_card?payment_type == "card"end
end

或者

class Account < ApplicationRecordvalidates :password, confirmation: true,unless: Proc.new { |a| a.password.blank? }
end

:if 和 :unless也可以同时验证

class Computer < ApplicationRecordvalidates :mouse, presence: true,if: [Proc.new { |c| c.market.retail? }, :desktop?],unless: Proc.new { |c| c.trackpad.present? }
end
5 自定义验证
class Invoice < ApplicationRecordvalidate :expiration_date_cannot_be_in_the_past,:discount_cannot_be_greater_than_total_valuedef expiration_date_cannot_be_in_the_pastif expiration_date.present? && expiration_date < Date.todayerrors.add(:expiration_date, "can't be in the past")endenddef discount_cannot_be_greater_than_total_valueif discount > total_valueerrors.add(:discount, "can't be greater than total value")endend
end
6. 在视图中显示验证错误

定义模型并添加验证后,当通过网络表单创建model时验证失败,需要显示信息。

由于每个应用程序处理显示验证错误不同,因此Rails不包含任何用于生成这些信息的视图助手,需要自己写。比如:

<% if @article.errors.any? %><div id="error_explanation"><h2><%= pluralize(@article.errors.count, "error") %> prohibited this article from being saved:</h2><ul><% @article.errors.each do |error| %><li><%= error.full_message %></li><% end %></ul></div>
<% end %>

http://www.lryc.cn/news/500515.html

相关文章:

  • JS听到了因果的回响
  • 【高中生讲机器学习】28. 集成学习之 Bagging 随机森林!
  • 硬件设计 | Altium Designer软件PCB规则设置
  • 【Elasticsearch】实现用户行为分析
  • python字符串处理基础操作总结
  • 电子商务人工智能指南 6/6 - 人工智能生成的产品图像
  • 【论文阅读】相似误差订正方法在风电短期风速预报中的应用研究
  • 贪心算法 - 学习笔记 【C++】
  • 精确的单向延迟测量:使用普通硬件和软件
  • 【MySQL 进阶之路】存储引擎和SQL优化技巧分析
  • vue+elementUI从B页面回到A页面并且定位到A页面的el-tabs的某个页签
  • {结对编程/大模型} 实践营项目案例 | 基于RAG搭建政策问答智能聊天助手
  • 【Canvas与图标】乡土风金属铝边立方红黄底黑字图像处理图标
  • 【开源】A064—基于JAVA的民族婚纱预定系统的设计与实现
  • C++实现一个经典计算器(逆波兰算法)附源码
  • Python知识分享第二十二天-数据结构入门
  • 【WRF理论第十三期】详细介绍 Registry 的作用、结构和内容
  • Android启动优化指南
  • 【ETCD】【源码阅读】configureClientListeners () 函数解析
  • IO进程学习笔记
  • 智能手机回暖:华为点火,小米荣耀OV拱火
  • Sqoop导入数据(mysql---->>hive)
  • 实验3-实时数据流处理-Flink
  • 深度学习实验十四 循环神经网络(1)——测试简单循环网络的记忆能力
  • k8s部署odoo18(kubeshpere面板)
  • 【模型对比】ChatGPT vs Kimi vs 文心一言那个更好用?数据详细解析,找出最适合你的AI辅助工具!
  • Java——容器(单例集合)(上)
  • 如何配置Github并在本地提交代码
  • 工作bug,keil5编译器,理解int 类型函数返回值问题,详解!!!
  • 简明速通Java接口