简体   繁体   English

Rails 4删除了一些验证错误消息,但不是全部

[英]Rails 4 remove some validation error messages but not all

I want my validation message to no matter what error be simply the following: 无论什么错误,我都希望我的验证消息仅是以下内容:

"example: (415) 944-2099" “示例:(415)944-2099”

I have the following in my model 我的模型中有以下内容

  validates :phonenumber, presence: {is: true, message: " "},
                          format: {with: VALID_PNUM_REGEX, message: "example: (415) 944-2099" },
                          length: {in: 10..15, message: " "}

The above code though displays the following (if the field is left blank, therefore not matching any of the requirements): ", example: (415) 944-2099," 上面的代码虽然显示以下内容(如果字段为空,因此不符合任何要求):“,例如:(415)944-2099,”

How do I get rid of the commas or simply make my own overall validation message? 如何摆脱逗号或简单地编写自己的整体验证消息?

Try this validator in your model: 在您的模型中尝试以下验证器:

validate :phone_validator

def phone_validator
  if phonenumber.blank? || !phonenumber.match(VALID_PNUM_REGEX) || !phonenumber.length.in?(10..15)
    self.errors.add(:phonenumber, 'example: (415) 944-2099')
  end
end

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

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