开发者

Making a custom validator with regex..not triggering yet

Hey guys, I thought this would be fairly straightforward, but it's not it seems..maybe I'm just writing the syntax wrong..

In my model I'm checking for certain key words

before_validation :deal_validation

def deal_validation
  if self.description.match /(exp\s|expire|ex\s|print|mention|\/)/ 
    errors.add(:description, "Now just a second! You can't use those words!")
  end    
end

But it doesn't seem to stop the saving of the model.

Am I missing something开发者_运维知识库?


Changed it to

validate :deal_validation

Works!


I don't really know ruby, but used rubular to test, and it seems like you want:

(\bexpire\b|\bexp?\b|\bprint\b|\bmention\b|\b\/\b)

the \b characters match word boundaries. as written, your regex will match anything with "/", as well as strings like "regex ". is that really what you want?

edited to improve it slightly using the "?" quantifier, which matches both "exp" and "ex"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜