数据验证

验证表单提供的数据是否符合要求
使用rails自带的内置辅助功能ActiveRecord Validation 来实现
https://ruby-china.github.io/rails-guides/active_record_validations.html

何时验证

ActiveRecord Validation 会在执行sql(即save/create/update)之前进行数据验证,如果数据不符合要求,不会去执行sql的写入操作,如 insert/update

注意事项:
不是所有的写入操作都会触发ActiveRecord Validation

# 以下方法会触发数据验证
save
create
update

# 以下方法不会触发数据验证
decrement
decrement_counter
increment
increment_counter
toggle
touch
update_all         
update_attribute
update_column
update_columns
update_counters

save(validate: false)  #如果save时指定validate: false, 也不会触发数据验证
项目中使用

继续我们的用户注册登录功能,在User中添加

class User < ApplicationRecord

  has_secure_password
  before_create {generate_token(:auth_token)}
  validates :name, :email, presence: true                        #presence 存在性
  validates :name, :email, uniqueness: { case_sensitive: false } # uniqueness 唯一 case_sensitive 区分大小写

  def generate_token column
    begin
      self[column] = SecureRandom.urlsafe_base64
    end while User.exists?(column => self[column])
  end
end

用户如果保存失败,可以使用errors来查看错误信息

u = User.create(name: 'whj')
# 因email为空,会产生rollback
# 使用 u.errors 会返回一个ActiveRecord::Errors 对象,其中message属性会提示错误原因

#  #<ActiveModel::Errors:0x007f938c898f10 @base=#<User id: nil, name: "whj", email: nil, nickname: nil, introduction: nil, password_digest: nil, created_at: nil, updated_at: nil, auth_token: nil>, @messages={:password=>["can't be blank"], :email=>["can't be blank"]}, @details={:password=>[{:error=>:blank}], :email=>[{:error=>:blank}]}> 
# 还记得吗, has_secure_password 也会对密码的存在性记性校验

使用errors在前端页面中显示用户注册失败原因

# controllers/users_controller.rb
def create
    @user = User.new(user_params)              #为了erb中能使用,user改为@user
    if @user.save                              #成功保存,不变
      cookies[:auth_token] = @user.auth_token
      redirect_to :root
    else
      render :signup                           #保存失败,重新渲染signup页面,已填写数据不会丢失
    end
 end
 
 # views/users/signup.html.erb
 
        <dl class="form">
        <dt><%= f.label :email %></dt>
        <dd><%= f.text_field :email %></dd>
            <% if @user.errors[:email].any? %>
              <dd class="error"><%= @user.errors[:email][0] %></dd>
            <% end %>
        </dl>
# 这样用户在提交失败后重新渲染signup页面就有错误提示了

项目中使用简单的ActiveRecord Validation 就是这样了,下面继续ActiveRecord Validation 的辅助方法

presence

项目中用到的存在性校验
除了属性的值是否存在,还可以验证关联对象是否存在

class LineItem < ApplicationRecord
  belongs_to :order
  validates :order, presence: true
end

class Order < ApplicationRecord
  has_many :line_items, inverse_of: :order   # 需要添加 inverse_of: :order 
end

uniqueness

唯一性校验
该方法不会在数据库中创建唯一索引
:scope 选项用于指定检查唯一性时使用的一个或多个属性
:case_sensitive 选项用于指定是否区分大小写,默认为true

acceptance

验证表单中的复选框是否被选中

confirmation

验证某个属性两次输入(如:再次确认)是否相同
confirmation 会创建一个名为 _confirmation 的虚拟属性,如:phone, phone_confirmation
缺陷: 只有在 _confirmation 属性不为nil时才会触发校验, 所以需要和persence一起使用

validates :phone, confirmation: true
validates :phone_confirmation, presence: true
exclusion

检查属性的值是否不在某个集合中
需要指定in选项或者with选项

validates :subdomain, exclusion: { in: %w(www us ca jp),  message: "%{value} is reserved." }
inclusion

检查属性的值是否在某个集合中
需要指定in选项或者within选项

validates :size, inclusion: { in: %w(small medium large),
    message: "%{value} is not a valid size" }
format

检查属性的值是否匹配 with指定的正则
如果使用了 without,则不能符合后面的正则


validates :legacy_code, format: { with: /\A[a-zA-Z]+\z/,
    message: "only allows letters" }
length

验证属性值的长度,有多个选项
1 :minimum:属性的值不能比指定的长度短;
2 :maximum:属性的值不能比指定的长度长;
3 :in(或 :within):属性值的长度在指定的范围内。该选项的值必须是一个范围;\

4 :is:属性值的长度必须等于指定值;

  validates :name, length: { minimum: 2 }
  validates :bio, length: { maximum: 500 }
  validates :password, length: { in: 6..20 }
  validates :registration_number, length: { is: 6 }
numericality

检测是否只包含数字
匹配的值是可选的正负符号后加整数或浮点数
numericality 默认不接受 nil 值。可以使用 allow_nil: true 选项允许接受 nil。

    only_integer  只接受整数
    greater_than  大于
    greater_than_or_equal_to  大于等于
    equal_to 等于
    less_than 小于
    less_than_or_equal_to 小于等于
    other_than 不等于
    odd  奇数
    even 偶数
validates_with

记录交给其他类做验证
validates_with 方法的参数是一个类或一组类,用来做验证


class GoodnessValidator < ActiveModel::Validator
  def validate(record)
    if record.first_name == "Evil"
      record.errors[:base] << "This person is evil"
    end
  end
end
 
class Person < ApplicationRecord
  validates_with GoodnessValidator
end

常用的选项

allow_nil
allow_blank
message
:on

指定什么时候才验证

  validates :email, uniqueness: true, on: :create
strict

严格验证\

通过 :strict 选项指定抛出什么异常:

  validates :name, presence: { strict: true }
Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐