Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to use custom filters inside model

Hi,

I created a custom filter inside my bootstrap:

use Phalcon\Filter;

$di->setShared('filter', function() {
    $filter = new Filter();

    $filter->add('commatodot', function ($value) {
        return preg_replace('/\./', ',', $value); 
    });

    return $filter;
});

I'm trying to access it through the static function getDefault():

$test = Di::getDefault()->getService('filter');

But hot can I use my specific filter?



35.7k
Accepted
answer
edited Jul '16

https://docs.phalconphp.com/en/latest/reference/filter.html#creating-your-own-filters

https://docs.phalconphp.com/en/latest/reference/models.html#validating-data-integrity

class SomeModel extends \Phalcon\Mvc\Model
{
    protected $column;
    public function setColumn($value)
    {
        $value = $this->getDI()->getShared('filter')->sanitize($value, 'commatodot');
        $this->column = $value;
        return $this;
    }
}