Rails 3:验证中的自定义错误消息

发布于 2024-09-24 09:01:29 字数 757 浏览 3 评论 0原文

我不明白为什么以下内容在 Rails 3 中不起作用。我收到“未定义的局部变量或方法‘custom_message’”错误。

validates :to_email, :email_format => { :message => custom_message }

def custom_message
  self.to_name + "'s email is not valid"
end

我也尝试过使用 :message => :custom_message 相反,按照 rails-validation-message-error 帖子中的建议,但没有运气。

:email_format 是位于 lib 文件夹中的自定义验证器:

class EmailFormatValidator < ActiveModel::EachValidator
  def validate_each(object, attribute, value)
    unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
      object.errors[attribute] << (options[:message] || 'is not valid')
    end
  end
end

I don't understand why the following is not working in Rails 3. I'm getting "undefined local variable or method `custom_message'" error.

validates :to_email, :email_format => { :message => custom_message }

def custom_message
  self.to_name + "'s email is not valid"
end

I also tried using :message => :custom_message instead as was suggested in rails-validation-message-error post with no luck.

:email_format is a custom validator located in lib folder:

class EmailFormatValidator < ActiveModel::EachValidator
  def validate_each(object, attribute, value)
    unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
      object.errors[attribute] << (options[:message] || 'is not valid')
    end
  end
end

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

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

发布评论

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

评论(3

时光是把杀猪刀 2024-10-01 09:01:29

仅供参考,我认为这就是正在发生的事情。 “validates”方法是一个类方法,即 MyModel.validates()。当您将这些参数传递给“validates”并调用“custom_message”时,您实际上是在调用 MyModel.custom_message。因此,您需要

def self.custom_message
  " is not a valid email address."
end

validates :to_email, :email_format => { :message => custom_message }

在调用验证之前定义类似于 self.custom_message 的内容。

Just for reference, this is what I believe is going on. The 'validates' method is a class method, i.e. MyModel.validates(). When you pass those params to 'validates' and you call 'custom_message', you're actually calling MyModel.custom_message. So you would need something like

def self.custom_message
  " is not a valid email address."
end

validates :to_email, :email_format => { :message => custom_message }

with self.custom_message defined before the call to validates.

疯狂的代价 2024-10-01 09:01:29

如果有人感兴趣,我想出了以下解决方案来解决我的问题:

模型:

validates :to_email, :email_format => { :name_attr => :to_name, :message => "'s email is not valid" }

lib/email_format_validator.rb:

class EmailFormatValidator < ActiveModel::EachValidator

  def validate_each(object, attribute, value)
    unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i

      error_message = if options[:message] && options[:name_attr]
        object.send(options[:name_attr]).capitalize + options[:message]
      elsif options[:message]
        options[:message]
      else
        'is not valid'
      end

      object.errors[attribute] << error_message
    end
  end
end

If anyone is interested I came up with the following solution to my problem:

Model:

validates :to_email, :email_format => { :name_attr => :to_name, :message => "'s email is not valid" }

lib/email_format_validator.rb:

class EmailFormatValidator < ActiveModel::EachValidator

  def validate_each(object, attribute, value)
    unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i

      error_message = if options[:message] && options[:name_attr]
        object.send(options[:name_attr]).capitalize + options[:message]
      elsif options[:message]
        options[:message]
      else
        'is not valid'
      end

      object.errors[attribute] << error_message
    end
  end
end
于我来说 2024-10-01 09:01:29

也许需要在验证之上定义方法“custom_message”。

Maybe the method "custom_message" needs to be defined above the validation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文