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
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]"
I've created a simple example https://gist.github.com/sergeyklay/78bf853c1d280681a145 with test https://gist.github.com/sergeyklay/201efa076834bc7d6134 for dot-notation
Any plans to add Arr to Phalcon ?
@mzf This is separated feature request
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"];