10-19-2020, 11:01 PM
Hi,
Can somebody tell me how to use FilterInterface in modifying request before using it in the Controller? I want to sanitize or to remove unwanted characters in the data before using it in my Controller.
Here is the sample of my code:
Filter:
Controller:
Is it possible to sanitize the value of number_only variable from "123abc456" to "123456"?
Thank you!
Can somebody tell me how to use FilterInterface in modifying request before using it in the Controller? I want to sanitize or to remove unwanted characters in the data before using it in my Controller.
Here is the sample of my code:
Filter:
PHP Code:
namespace App\Filters;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Filters\FilterInterface;
class SanitizeFields implements FilterInterface
{
protected $session;
public function before(RequestInterface $request, $arguments = null)
{
$request->getVar('number_only', FILTER_SANITIZE_NUMBER_INT);
}
public function after(RequestInterface $request, ResponseInterface $response, $arguments = null)
{
// Do something here
}
}
Controller:
PHP Code:
//controller
public function getData()
{
echo $this->request->getVar('number_only');
}
Is it possible to sanitize the value of number_only variable from "123abc456" to "123456"?
Thank you!