标题不是必填字段 我只想验证它何时不是nil(仅当用户键入内容时)
validates_format_of :title, :with => /\A[A-Za-z0-9_]+\z/, :message => "must be half-width English numbers and letters, and_"
然而,一旦我设置了它,标题字段就成了必填字段 如何设置它只在标题字段不是nil时验证?
答案 0 :(得分:2)
我认为这样可行:
validates_format_of :title, :with => /\A[A-Za-z0-9_]+\z/, :message => "must be half-width English numbers and letters, and_", :if => lambda{ !self.title.nil? }