Skip to content

Instantly share code, notes, and snippets.

@cebe
Created February 22, 2013 17:54
Show Gist options
  • Save cebe/5015310 to your computer and use it in GitHub Desktop.
Save cebe/5015310 to your computer and use it in GitHub Desktop.
Allows to filter attributes before validation: trim,escape,re-format etc...
<?php
/**
* Allows to filter attributes before validation: trim,escape,re-format etc...
*
* List of implemented filters:
* - trim
*
* How to use:
*
* 1. Add the behavior to your model:
* <pre>
* public function behaviors()
* {
* return array(
* 'filter' => array(
* 'class' => 'application.extensions.ActiveRecordFilterBehavior',
* 'inputFilters' => array() // configure filters here
* ),
* );
* }
* </pre>
*
* 2. configure inputFilters property like this:
* <pre>
* array(
* 'attributeName' => filterDefinition
* 'attribute1,attribute2,attribute3' => filterDefinition
* )
* where filterDefinition can be string or array:
* 'trim' - add one filter 'trim'
* array('trim') - add one filter 'trim'
* array('trim', 'escape') - add filters 'trim' and 'escape'
* array('trim' => array( - add filters 'trim' and 'escape'
* 'charlist' => ' _-' and give parameter 'charlist' with ' _-' to filter trim
* ),
* 'escape'
* )
* </pre>
*
* Example:
* <pre>
* 'inputFilters' => array() // configure filters here
* return array(
* 'userName' => array('trim', 'escape', 'replace' => array('pattern' => '/\s+/', 'value' => '_' ))
* 'field1, field3, fieldX' => 'trim',
* 'userName' => array('trim' => array('charlist' => ' -_'), 'escape')
* 'field1, field3, fieldX' => array('trim', 'escape'),
* );
* }
* </pre>
*
* @author Carsten Brandt <[email protected]>
*/
class ActiveRecordFilterBehavior extends CModelBehavior
{
/**
* @var array list of inputFilters to apply
* see class description of {@see ActiveRecordFilterBehavior} for details.
*/
public $inputFilters = array();
/**
* Responds to {@link CModel::onBeforeValidate} event.
*
* Applies configured filters
*
* @param CModelEvent $event event parameter
* @throws CException
*/
public function beforeValidate($event)
{
$filters = $this->inputFilters;
// allow array keys like 'name,phone,field' => 'trim'
foreach($filters as $attribute => $filter)
{
$attributes = explode(',', $attribute);
if (count($attributes) > 1) {
// add all attributes from csv
foreach($attributes as $newAttribute) {
$filters[trim($newAttribute)] = $filter;
}
// remove the csv key
unset($filters[$attribute]);
}
}
// apply filters
foreach($filters as $attribute => $filter)
{
if ($this->owner->hasAttribute($attribute))
{
if (!is_array($filter)) {
$filter = array($filter);
}
foreach($filter as $filterName => $filterData)
{
if (is_string($filterData)) {
$filterName = $filterData;
$filterData = array();
}
switch($filterName)
{
case 'trim':
if (is_string($this->owner->$attribute)) {
if (isset($filterData['charlist'])) {
$this->owner->$attribute = trim($this->owner->$attribute, $filterData['charlist']);
} else {
$this->owner->$attribute = trim($this->owner->$attribute);
}
}
break;
default:
throw new CException('Unknown filter name "' . $filterName . '".');
break;
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment