Welcome Guest, Not a member yet? Register   Sign In
Display a set_message for each bad wor found in string
#1

When the user types in a couple of word lets say ban and bad in string.

How could I get my set_message() to out put error message for each word found.

Currently only displays one message.


PHP Code:
<?php

class MY_Form_validation extends CI_Form_validation {

    public function 
badword($string) {
        
// Dummy words
        
$badWords = array("ban","bad","user","pass","stack","name","html");
        
$matches = array();
        
$matchFound preg_match_all("/\b(" implode($badWords,"|") . ")\b/i"$string$matches);

        if (
$matchFound) {
         
     
              $words 
array_unique($matches[0]);
         
     
              
foreach($words as $word) {
         
         $this->set_message('badword''Your message has a band word <b>' $word '</b> you can not use that word.');
         
     }

         
     return false;
        
        } else {

            return 
true;
        }

    }

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

(This post was last modified: 09-30-2016, 02:17 AM by RogerMore.)

Hey Wolfgang,

I can't help you with a solution for multiple messages.
What I can say is that your for loop is setting the message for 'badword' over and over again. So you probably get only one mesage with the last banned word in it.

Why not iterate through the matches and build a string with all the found bad words? Say $found_words becomes something like 'ban, bad'.
Then you can set a message like: 'You have used one or more banned words. You can't use $found_words. Sorry!'

Hope it helps!

-Roger
Reply
#3

(This post was last modified: 09-30-2016, 02:21 AM by salain.)

I think using set_message in a loop overwrite the previous message.
So you must be getting only the message for the last bad word.

I would try to implode the word array to test.

PHP Code:
$this->set_message('badword''Your message has band word(s) <b>' implode(',',$words) . '</b> you can not use that word.'); 
A good decision is based on knowledge and not on numbers. - Plato

Reply
#4

Yeah, that's basically what I said...  Tongue
Reply
#5

(This post was last modified: 09-30-2016, 05:44 PM by InsiteFX. Edit Reason: fix badwords mistake. )

Rough try, not tested see if it will get you started.

Old saying get it to work first then throw in all the bells and whistles.

PHP Code:
class MY_Form_validation extends CI_Form_validation {

 
   public function badword($string)
 
   {
 
       // Dummy words
 
       $badWords = array("ban","bad","user","pass","stack","name","html");

 
       $badFound '';
 
       $badCount 0;

 
       $matches  = array();
 
       $matchFound preg_match_all("/\b(" implode($badWords,"|") . ")\b/i"$string$matches);

 
       if ($matchFound)
 
            
              $words 
array_unique($matches[0]);
 
             
              foreach
($words as $word)
 
             {
 
                 $badFound =. $word ' ';
 
                 $badCount++;
 
             }
 
             
              if 
($badCount == 1)
 
             {
 
                 $this->set_message('badword''Your message has a band word <b>' $badFound '</b>you can not use that word.');
 
             }
 
             else
              
{
 
                 $this->set_message('badword''Your message has a band words <b>' $badFound '</b>you can not use those words.');
 
             }

 
             return false       
        
}
 
       else
        
{
 
           return true;
 
       }
 
   }

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

Looks like the solution of InsiteFX will do the trick, but there is one tiny thing you have to change.

In the else section where the message is set for multiple bad words, the message has to be set to 'badword' and not 'badwords', since your validation function is called badword.

Hope this helps!

-Roger
Reply
#7

(09-30-2016, 04:17 AM)InsiteFX Wrote: Rough try, not tested see if it will get you started.

Old saying get it to work first then throw in all the bells and whistles.

PHP Code:
class MY_Form_validation extends CI_Form_validation {

 
   public function badword($string)
 
   {
 
       // Dummy words
 
       $badWords = array("ban","bad","user","pass","stack","name","html");

 
       $badFound '';
 
       $badCount 0;

 
       $matches  = array();
 
       $matchFound preg_match_all("/\b(" implode($badWords,"|") . ")\b/i"$string$matches);

 
       if ($matchFound)
 
            
              $words 
array_unique($matches[0]);
 
             
              foreach
($words as $word)
 
             {
 
                 $badFound =. $word ' ';
 
                 $badCount++;
 
             }
 
             
              if 
($badCount == 1)
 
             {
 
                 $this->set_message('badword''Your message has a band word <b>' $badFound '</b>you can not use that word.');
 
             }
 
             else
              
{
 
                 $this->set_message('badwords''Your message has a band words <b>' $badFound '</b>you can not use those words.');
 
             }

 
             return false       
        
}
 
       else
        
{
 
           return true;
 
       }
 
   }



Thank you
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#8

(09-30-2016, 02:20 AM)salain Wrote: I think using set_message in a loop overwrite the previous message.
So you must be getting only the message for the last bad word.

I would try to implode the word array to test.

PHP Code:
$this->set_message('badword''Your message has band word(s) <b>' implode(',',$words) . '</b> you can not use that word.'); 

Thank you
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#9

@wolfgang1983

PHP Code:
// should this
$this->set_message('badword''Your message has a band word <b>' $badFound '</b>you can not use that word.');

// be like this?
$this->set_message('badword''Your message has a bad word <b>' $badFound '</b>you can not use that word.'); 

The first one is yours but it says band word.

Should it not be bad word?
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB