Yii Framework Forum: Custom validation - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Custom validation Is it possible to detect any offensive words in textarea? Rate Topic: -----

#1 User is offline   m1m1m1 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 23-May 10

Posted 01 November 2010 - 07:48 AM

Hi,
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,
Marcin
0

#2 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 01 November 2010 - 08:21 AM

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!
0

#3 User is offline   m1m1m1 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 23-May 10

Posted 01 November 2010 - 09:12 AM

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

#4 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 01 November 2010 - 10:04 AM

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!
1

#5 User is offline   m1m1m1 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 23-May 10

Posted 01 November 2010 - 10:32 AM

I will try it.. thank you :)
0

#6 User is offline   ignis 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 57
  • Joined: 27-April 10
  • Location:Lithuania - Vilnius

Posted 01 November 2010 - 01:55 PM

u can use a capcha for escaping spam
0

#7 User is offline   m1m1m1 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 23-May 10

Posted 01 November 2010 - 03:19 PM

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

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users