Here is a way to trim all variables received through $_POST and $_GET.
There are good reasons to trim user input: you do not need the spaces in the database, it avoids comparison problems, white space is probably unwanted.
A HelperClass contains the following code:
public static function trimPostGet() {
array_walk_recursive($_POST, function (&$val) { $val = trim($val); });
array_walk_recursive($_GET, function (&$val) { $val = trim($val); });
}
In the application’s configuration file:
'onBeginRequest' => array('HelperClass', 'trimPostGet'),
Any comment on using such method is welcome. Maybe you see some serious drawbacks ?
Other methods that are more Yii specific are:
-
a filter in a validation rule
-
beforeValidate of the model
but then you would have to do this for each and every input!