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

Array notation in naming Form elements

I'm using array notation in naming my forms.

And I want to use the Phalcon\Forms\Form class to do as much as I can.

In my view I'm naming (I'll use in the future the Same) an element in this way :

<?php echo $form->render('api[name]');?>

The rendering result, reading the source code doing a GET request is:

<input type="text" class="form-control" name="api[name]" value="">

Which is what I was expecting.

Now the problem is when I'm trying to submit, since the validation is not working, and I think the problem is just with this array notation because for the other elements the validation works fine.

I have a class that extends from Phalcon\Forms\Form like:

class BricksForm extends Form
{
    public function initialize($entity = null, $options = null)
    {
        $apiName = new Text('api[name]');
        $apiName->addValidators([
            new PresenceOf([
                'message' => 'The API name is required'
                    ]),
            new StringLength([
                'min' => 3,
                'message' => 'The Name for the API has to have at least 3 characters'
                    ]),
        ]);
        $apiName->setAttributes(['class' => 'form-control']);
        $this->add($apiName);
    }
}

And when I try to submit my POST data, I'm getting errors messages for the Phalcon\Validation\Validator\PresenceOf and Phalcon\Validation\Validator\StringLength for that Phalcon\Forms\Element\Text

Am I doing something wrong? I think maybe there is a problem in the naming convention for forms. I haven't seen examples in Phalcon using array notation on elements.

Any idea?

Many thanks!

Can you edit your post to utilise the code formatting?

See the code blocks section: https://forum.phalcon.io/help/markdown