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

Form element name

Hi,

I tried to use array notation for element name:

$name = new Element\Text('test[name]');

but it doesn't work. Another elements are populated from request but this field not. When I tried to use element name without 'test[]', only 'name', it works. Is it bug or normal behavior for phalcon form?

Thanks



1.0k

try

$name = new Element\Text('something', ['name' => 'test[name]']);

when not defining any values specific it uses the same value of all attributes (id,class,name) and the id used to call the element. These values does not accept arrays

Naming works correctly, this is not a problem. But prefilling is broken for test[name].



7.5k

Nobody knows?



8.1k
edited Jun '14

When naming your elements that way you get an actual array in your $_GET. So there doesn't exist an $_GET var with the name test[name] but there is an array test with contains name as an index.

AFAIK Phalcon can't / doesn't deal with that internally. So you have to translate that to your actual form element name test[name] again, and pass that, along with the other values from the $_GET, to the Form as it's entity.

Make sure to cast your array of key/values to an object ((object) $myArray) when passing it to the Form.

I guess this is the issue you're coping with?

edit: of course you can also use Phalcon\Tag::setDefault() And the above should also apply for $_POST.



7.3k
Accepted
answer

Ok, so we got this fix for now.. Somewhere early, ie. in BaseController - initialize(); we are calling this:

    /**
     * Hopefully, this method won't be needed one day.
     * It finds all arrays in request, makes "inline" array representation 
     * for them and appends it back to request. So elements named like xxx[yyy] 
     * are prefilled by Phalcon correctly from this moment.
     */
    protected function repairRequestForPhalcon() {

        $newKeys = array();
        $iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($this->request->get()));
        foreach ($iterator as $value) {
            $newKey = null;
            for ($i = 0; $i <= $iterator->getDepth(); $i++) {
                if ($i != 0) {
                    $newKey .= "[" . $iterator->getSubIterator($i)->key() . "]";
                } else {
                    $newKey .= $iterator->getSubIterator($i)->key();
                }
            }
            $newKeys[$newKey] = $value;
        }
        // Yes, exactly. We are appending new fake keys to actual $_REQUEST, 
        // so $request->get() will find them there. Phalcon Request object 
        // doesn't have any public functionality for changing his inner representation;
        // or at least we don't know how to do it
        $_REQUEST += $newKeys;
    }

So the request looks like this now:

array {
    _url => "/admin/user/list"
    grid => array {
        user => array {
            filter => array {
                text => "someText"
                state => "3"
            }
        }
    }
    grid[user][filter][text] => "someText"
    grid[user][filter][state] => "3"
}

I think Phalcon should understand arrays in request internally, or leave the prefilling for the users completely, otherwise there is a big confusion. Any ideas how to avoid or improve this are very welcomed.