一些验证通过后的自定义方法

发布于 2024-12-01 00:24:56 字数 420 浏览 1 评论 0原文

好的。

1)我需要验证模型中的 :link ,并且仅当它不为空(或 nil)时才执行此操作。

2) 如果 :link 不为空且标准验证通过 — 我需要运行自定义验证方法来检查 URL 可用性。

通过“标准”验证,我的意思是这样的:

validates :link, :presence => true, :uniqueness => true, 
                 :format => { :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix }

实现这个的正确方法是什么?

Ok.

1) I need to validate :link in my model and do that only if it is not blank (or nil).

2) If :link is not blank and standard validation passes — I need to run my custom validation method to check URL availability.

By "standard" validation I mean something like this:

validates :link, :presence => true, :uniqueness => true, 
                 :format => { :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix }

What is the correct way to implement this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

物价感观 2024-12-08 00:24:56

仅当链接不为空时,它才会检查模型中的验证:

validates_presence_of :link, :uniqueness => true, 
                      :format => { :with => /^(http|https)://[a-z0-9]+([-.]{1}[a-z0-9]+).[a-z]{2,5}(:[0-9]{1,5})?(/.)?$/ix }, :if => :link_present?

def link
  self.link
end

def link_present?
  link.present?
end

It checks for validation in your model only if link isn't blank:

validates_presence_of :link, :uniqueness => true, 
                      :format => { :with => /^(http|https)://[a-z0-9]+([-.]{1}[a-z0-9]+).[a-z]{2,5}(:[0-9]{1,5})?(/.)?$/ix }, :if => :link_present?

def link
  self.link
end

def link_present?
  link.present?
end
驱逐舰岛风号 2024-12-08 00:24:56

好的。在朋友的帮助下我终于解决了这个问题。

class Post < ActiveRecord::Base

  # skipped some things not necessary 

  validates_format_of :link, :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix , :allow_blank => true
  validates_length_of :link, :maximum => 2000
  validates_uniqueness_of :link, :allow_blank => true

  validate :ensure_link_is_available, :if => proc{|post| post.link.present? && post.errors.empty?}

  def ensure_link_is_available
    begin
      require "net/http"
      url = URI.parse(self.link)
      req = Net::HTTP.new(url.host, url.port)
      res = req.request_head(url.path)
    rescue
      # error occured, add error
      self.errors.add(:link, 'The requested URL could not be retrieved')
    else
      # valid site
      if (res.code.to_i > 308) 
        error_message = 'Server responded with ' + res.code
        self.errors.add(:link, error_message)
      end
    end
  end                    

end

Ok. With friends help I finally solved this.

class Post < ActiveRecord::Base

  # skipped some things not necessary 

  validates_format_of :link, :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix , :allow_blank => true
  validates_length_of :link, :maximum => 2000
  validates_uniqueness_of :link, :allow_blank => true

  validate :ensure_link_is_available, :if => proc{|post| post.link.present? && post.errors.empty?}

  def ensure_link_is_available
    begin
      require "net/http"
      url = URI.parse(self.link)
      req = Net::HTTP.new(url.host, url.port)
      res = req.request_head(url.path)
    rescue
      # error occured, add error
      self.errors.add(:link, 'The requested URL could not be retrieved')
    else
      # valid site
      if (res.code.to_i > 308) 
        error_message = 'Server responded with ' + res.code
        self.errors.add(:link, error_message)
      end
    end
  end                    

end
温柔嚣张 2024-12-08 00:24:56
validates_format_of :url_field, :with => URI::regexp(%w(http https))
validates_format_of :url_field, :with => URI::regexp(%w(http https))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文