phalcon

Filtering and Sanitizing

Convenient in-model sanitizing

Set a convenience method in your base model

namespace Base;

class Model extends \Phalcon\Mvc\Model
{
    public function sanitize($attr, $filterName)
    {
        $filter = $this->getDI()->get('filter');
        $this->$attr = $filter->sanitize($this->$attr, $filterName);
    }
}

Then use like so

class User extends \Base\Model
{
    public function beforeValidation()
    {
        $this->sanitize('id', 'int');
        // input $this->id: 123abc
        // output: 123

        $this->sanitize('email', 'email');
        // input $this->email: youre(-)mail@dom/ain.com
        // output: youremail@domain.com
        
        $this->sanitize('wage', 'float');
        // input $this->wage: +1234ab.56cd
        // output: 1234.56

        $this->sanitize('name', 'string');
        // input $this->name: <john>
        // output: john
    }
}

This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow