简体   繁体   English

仅在某些方法上验证

[英]validate only on some method rails

Based on rails validation docs . 基于rails验证文档 I need to validate fullname field only on update 我只需要在更新时验证fullname

# encoding: utf-8
class User < ActiveRecord::Base
  GENDER_MALE = true
  GENDER_FEMALE = false

  attr_accessor :password_confirm,
                :term,
                :year, :month, :day,
                :captcha

  validates :username, presence: {message: "Bạn phải nhập tài khoản"},
                      uniqueness: {message: 'Tài khoản đã tồn tại'}, :on => :update
  # validates :password, presence: {message: "Bạn phải nhập mật khẩu"},
  #                     confirmation: {message: 'Mật khẩu không chính xác'}
  # validates :password_confirmation, presence: {message: "Bạn phải nhập xác nhận mật khẩu"}
  # validates :fullname, presence: {message: "Bạn phải nhập họ tên"}
  # validates :email, presence: {message: "Bạn phải nhập email"},
  #                   uniqueness: {message: "Email đã tồn tại"}
  # validates :email, format: {with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i, message: "Email không đúng định dạng"},
  #                   unless: "email.blank?"
  # validates :term, acceptance: {message: "Bạn phải đồng ý điều khoản"}
  # # validates :gender, acceptance: {accept: [0,1], message: "Giới tính không hợp lệ"}
  # validate :_birthday_validator
  # validate :_captcha_validator
  #
  # before_save :_encrypt_password

  def signup
    self.birthday = "#{year.to_s}-#{month.to_s}-#{day.to_s}"
    self.save
  end

  def self.human_attribute_name(attr, option = {})
    "" || super
  end



  protected
    def _encrypt_password
      self.password = Digest::MD5::hexdigest(password)
    end

  private
    def _birthday_validator

      unless year.present? && month.present? && day.present?
        errors.add(:birthday, 'Bạn phải nhập ngày sinh')
      else
        errors.add(:birthday, 'Ngày sinh không hợp lệ') unless Date.valid_date?(year.to_i, month.to_i, day.to_i)
      end
    end

    def _captcha_validator
      if !(captcha.nil?)
        errors.add(:captcha, "Mã xác nhận không hợp lệ") if captcha == false
      end
    end
end

As understand, this validation rule only run when I call update method, but I have no idea why this rule run all the time 可以理解,该验证规则仅在我调用update方法时运行,但我不知道为什么该规则始终运行 在此处输入图片说明 Can anyone tell me why or I missed somethings? 谁能告诉我为什么还是我错过了什么?

Ps: Can Rails validates only for user defined method, somethings like validates :username, presence: true, only: [:my_func] Ps:Rails只能对用户定义的方法validates :username, presence: true, only: [:my_func] ,类似于validates :username, presence: true, only: [:my_func]

One way would be to set a virtual attribute which you'll only populate in the signup method: 一种方法是设置一个仅在signup方法中填充的虚拟属性

#app/models/user.rb
class User < ActiveRecord::Base
   attr_accessor :should_validate
   validates :fullname, presence: true, on: :update, if: "should_validate.present?" 
end

This way, you can then assign a value to should_validate only when you use signup : 这样,您就可以仅在使用signup时才将一个值分配给should_validate

def signup
   self.birthday = "#{year.to_s}-#{month.to_s}-#{day.to_s}"
   self.should_validate = true
   self.save
end

you can use method like 您可以使用类似的方法

validate :fullname , on: :update

def fullname
   if self.fullname.present?
      true
   else
      false
   end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM