-
wolfgang1983
Senior Member
-
Posts: 627
Threads: 271
Joined: Oct 2014
Reputation:
7
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!
-
RogerMore
CodeMonkey by day, ShaolinWarrior by ...
-
Posts: 70
Threads: 2
Joined: Oct 2014
Reputation:
8
09-30-2016, 02:15 AM
(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
-
salain
Member
-
Posts: 135
Threads: 2
Joined: Nov 2014
Reputation:
12
09-30-2016, 02:20 AM
(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
-
InsiteFX
Super Moderator
-
Posts: 6,502
Threads: 323
Joined: Oct 2014
Reputation:
239
09-30-2016, 04:17 AM
(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 )
-
RogerMore
CodeMonkey by day, ShaolinWarrior by ...
-
Posts: 70
Threads: 2
Joined: Oct 2014
Reputation:
8
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
-
wolfgang1983
Senior Member
-
Posts: 627
Threads: 271
Joined: Oct 2014
Reputation:
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!
-
wolfgang1983
Senior Member
-
Posts: 627
Threads: 271
Joined: Oct 2014
Reputation:
7
(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!
-
InsiteFX
Super Moderator
-
Posts: 6,502
Threads: 323
Joined: Oct 2014
Reputation:
239
@ 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 )
|