We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

request->getPost() not casting to int

I've got a text field that should only accept integers. To ensure integers server-side, I run a filter:

$barcode = $this->request->getPost('barcode','int');

However, with an input string of:

-102009260052016-818051805180518061806-071808-101810-111811-121812181218121812181218121812-131813-2018051805--710------------------------------18---------------------------------------------------------------234234--------------------------------------13---------------------------------------------------18051805111805--------------------217---------------22---1122--23-----------------18----2-2-------------1------251805--------------------------------------------------1805-1805-6-1805-------1805-----------------------------------------15----------------------160000---301805--------------------------------18061806---------------------------------------------------1806-1806--800800----------------1806-07-----------1234567------1806---------18051806-1805261807------480000300001500015000070000100001000001807--------22---------1805-------------27--------18081809-1808-101809----1809------18091805-1809----------1808-1809-----17123--1-2-3---23--2-2---2412-327-379--1809-----1809-1805-----1809-10------------------------

The filter has no effect. Manually casting to (int) does work though.

Is this a bug with Phalcon or with me. Running 3.3.1



93.7k
Accepted
answer
edited Feb '18

The second parameter is not casting, but "filter"-ing.

So according to the docs: https://docs.phalcon.io/de/3.2/filter you are the problem :)

int - Remove all characters except digits, plus and minus sign.

I guess either you have to use PHP casting or make a custom filter.

edited Apr '18

Hey @Dylan!

Was just browsing the docs and I saw this in the filters table:

int! Convert the value to an integer value using intval function

https://docs.phalcon.io/en/3.3/filter#types-of-builtin-filters

That's exactly what I'm looking for. Thanks for pointing it out.