Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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?



4.5k
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.