Cphalcon: request->getPost() dont work with arrays

Created on 30 Mar 2016  路  14Comments  路  Source: phalcon/cphalcon

If i use name of element in form like
name[index1][index2]
and then do

$values = $request->getPost('name[index1][index2]');

$values is NULL, but var_dump($_POST) show that it is not empty
I use Phalcon 2.1

not a bug

Most helpful comment

as @sergeyklay said getPost expects key as string, so :

$values = $request->getPost("name");

and then you can use $values as a normal array

$index1 = $values["index1"];

All 14 comments

New feature request?

getPost expects key as string.

$key = "name[index1][index2]";
$request->getPost($key);

You really think that this should work?

Well if you use:

$values = $request->getPost()['name']['index1']['index2'];

It's working ?

as @sergeyklay said getPost expects key as string, so :

$values = $request->getPost("name");

and then you can use $values as a normal array

$index1 = $values["index1"];

@Jurigag Yeah, in PHP >= 5.5 it will work.

@mzf getPost is designed in such a way to get something by a key and that key must be a string. So, it is not a bug ("dont work with arrays"), but your request for a feature.

But in any case I dislike such bit weird notation (something['key']['subkey']). The something.key.subkey syntax is more readable.

@sergeyklay Dot notation more usable, you are right

@Jurigag, @bkirova in your way you dont have filters and default values

@mzf If you need filters and default values you have to use Phalcon\Forms

@bkirova very interested, how?
P.S. Validators also dont work with array notation

You can use this
$this->request->getPost('action', array('striptags', 'trim', 'string'), null)

/**
     * Gets a variable from the $_POST superglobal applying filters if needed
     * If no parameters are given the $_POST superglobal is returned
     *
     *<code>
     *  //Returns value from $_POST["user_email"] without sanitizing
     *  $userEmail = $request->getPost("user_email");
     *
     *  //Returns value from $_POST["user_email"] with sanitizing
     *  $userEmail = $request->getPost("user_email", "email");
     *</code>
     */
    public function getPost(string! name = null, var filters = null, var defaultValue = null, boolean notAllowEmpty = false, boolean noRecursive = false) -> var
    {
        return this->getHelper(_POST, name, filters, defaultValue, notAllowEmpty, noRecursive);
    }

@googlle we talk about using array notation in name.
In your example simple name, not array notation like "name[action]"

Any plans to add Arr to Phalcon ?

@mzf This is separated feature request

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alvassin picture alvassin  路  3Comments

mynameisbogdan picture mynameisbogdan  路  3Comments

ismail0234 picture ismail0234  路  3Comments

bestirani2 picture bestirani2  路  3Comments

abcpremium picture abcpremium  路  3Comments