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

Multiple forms on one page

How to add two or more forms to one page? I do not know how to do that. I am getting some notice error.

$datepicker_form = new DatePickerForm();
$search_form = new BookingSearchForm();

$this->view->form = $datepicker_form;
$this->view->search = $search_form;

//and on view
{{ form('admin/index', 'method': 'post') }}
{{ search('admin/searchbookings', 'method': 'post') }}

If I can't add two or more forms with form builder that would be kind of silly, don't you think ? Why then use form builder at all?



7.8k
Accepted
answer
edited Aug '14

I was able to solve this by adding multiple forms to form manager:

//index.php
$di['forms'] = function() {
    return new \Phalcon\Forms\Manager();
};

And in some controller:

$this->forms->set('datepicker', new DatePickerForm());
$this->forms->set('search', new BookingSearchForm());

And on view:

{{ forms.get('datepicker').render('somefiled') }}
{{ forms.get('search').render('somefiled') }}

Is this only way that this can be solved, Anyone?

You can just use form() for create the form tag twice rather than use search(), also the method:post isn't required as this is done by default.