gpt4 book ai didi

ruby-on-rails - validates_length_of :maximum and :minimum on ruby on rails 的自定义错误消息

转载 作者:太空宇宙 更新时间:2023-11-03 17:22:49 27 4
gpt4 key购买 nike

我想为 :minimum 和其他为 :maximum 编写自定义错误消息

我有以下代码行

validates_length_of :user_name, :minimum=>5,:maximum=>30

ej if the length of :user_name is < 5 "The user_name must to have 5 characters as minimum"
ej if the length of :user_name is > 30 "The user_name must to have 30 characters as maximum"

最佳答案

较新的表示法是:

validates :user_name,
length: {
minimum: 5,
maximum: 30,
too_short: "The user name must have at least %{count} characters.",
too_long: "The user name must have no more than %{count} characters."
}

the documentation 中列出了更多示例.

旧的 validates_length_of 类型方法保留在那里用于遗留支持,但可能应该在新代码中避免使用。

关于ruby-on-rails - validates_length_of :maximum and :minimum on ruby on rails 的自定义错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26999800/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com