在还定义了格式的情况下,如何接受参数的 nil 值?
How can I accept the nil value of a param while there is also a format defined?
这是验证:
validates :phonenumber, allow_nil: true, format: { with: /\A\d{10}\z/, message: "not valid" }
当我没有在 phonenumber 中输入任何值时,它给我错误 "phonenumber not valid"。我该如何解决这个问题?
我猜你需要使用 allow_blank 选项:
validates :phonenumber,
format: { with: /\A\d{10}\z/, message: "not valid" },
allow_blank: true
这是验证:
validates :phonenumber, allow_nil: true, format: { with: /\A\d{10}\z/, message: "not valid" }
当我没有在 phonenumber 中输入任何值时,它给我错误 "phonenumber not valid"。我该如何解决这个问题?
我猜你需要使用 allow_blank 选项:
validates :phonenumber,
format: { with: /\A\d{10}\z/, message: "not valid" },
allow_blank: true