What is the best way to have some kind of filter over text columns

84 views Asked by At

I want to have some kind of filter on all of my text fields on my application, to know if a field has some bad content, like filter out bad mouthing of users.

I have thought of some ways

  • I could preg_replace() over php://input stream, and replace bad content
  • I could write a custom filter and put it for every single field in every model, which is a LOTand I really don't want to go there
  • I could some how override default sting validator of yii ( I don't know how to do that... yet)

I want to do that in a dynamic way and don't want to write a lot of rules

what is the best solution for my situation( from above or any other)?

Update: I'm looking for a way to replace cursing and swearing from the users input and replace it with something else

2

There are 2 answers

3
Jenno Richi Benat On

Hope this is what you are asking

You want to remove

  • Scripts in the Fields
  • Tags in the fields, etc

You can go with the Yii Input Extension. It removes unwanted data from the user's data.

You have two ways to do it

  • for each form in the controller
  • For all the forms in the application globally in config/main.php

In case done globally and when you use some CKEditor or tinyeditor you wont get required html posted. But then you have methods to get them also

EDIT:

Your case is to remove the unwanted words in the Users input

so

protected function beforeAction($action) {
    //Write your logic here with the posted variables.
    return parent::beforeAction($action);
}

This can be once in all the controller (sure this would work).

(NOT TRIED) Another way is to have it in the parent controller which is Controller

0
Sebastián Thierer On

If your columns are over models you can use something like this:

public function rules()
{
    $purifier = getHtmlPurifier();
    return array(
        array('column', 'filter', 'filter' => array($obj = $purifier, 'purify')),
    );
}

If you don't like to use htmlPurifier, you can create a filter on any other type by creating a function.