Custom validation


I have a form and I would like to detect any offensive words (spam) in it. If the user puts an offensive word in the form the validator will rise an error.

What is the easiest way to do that? :)

Best regards,


You can create a table "offensive_words" and then write a validation function for check if there are offensive words in the text.

But pay attention to the locale language!

ok, but how to connect validator rules with selecting words from database? :)

Add a rule like:

array('text', 'goodManner')

And then you can write your goodManner function:

public function goodManner()


   $words= explode($this->text);

   foreach ($words as $word)


      if ($word is a bad word)

         $this->addError('text', 'You should wash your mouth with soap!');



Instead of "is a bad word" you should query the database for check if the word is in the vocalbulary of bad words.

Just a sample, good work!

I will try it… thank you :)

u can use a capcha for escaping spam

well, I’m not sure if a capcha will allow me to prevent people from using some offensive words in their posts :)