Manually submit form in presenter

Notice: This thread is very old.
dimkalinux
Member | 24
+
0
-

Is exists any better method for manually submit form?
Its search presenter and i want that links like http://localhost/search/?… fire submit event for search form. Or its better dont use form in this case? Thanks.

<?php

final class SearchPresenter extends BasePresenter
{
    public function renderDefault()
    {
        $query = (string) $this->container->httpRequest->getQuery('query');
        if ($query !== "") {
            $form = $this->createComponentSearchForm();
            if ($form->isValid()) {
                $this->searchFormSucceeded($form);
            }
        }
    }

    protected function createComponentSearchForm()
    {
        $form = $this->searchFormFactory->createSearchForm();
        $form->onSuccess[] = $this->searchFormSucceeded;

        $form->setDefaults(array('query' => $this->container->httpRequest->getQuery('query')));

        return $form;
    }

    public function searchFormSucceeded($form)
    {
        $query = $form['query']->getValue();
        $itemIds = $this->searchService->findForUser($this->currentUser, $query);

        $this->template->items = array();
        if (!empty($itemIds)) {
            $this->template->items = $this->itemService->findById($itemIds);
        }
    }
}
<?php

class SearchFormFactory extends NetteObject
{
    public function createSearchForm()
    {
        $form = new Form;
        $form->setMethod('GET');

        $form->addText('query', 'Search')
             ->setAttribute('placeholder', 'Search')
             ->setAttribute('autofocus', '');

        $form->addSubmit('submit', 'Search');

        return $form;
    }
}

Last edited by dimkalinux (2014-03-11 20:56)

Oggy
Member | 306
+
0
-

I'm not certain what's your goal. If I comprehend it right you want to have the same result after processing the search form such as after http request with query parameter?

I don't think it's the right way to do that by manually submitting the form. Why don't have a function which will return a designated result. And you would call this function from searchFormSuccess as well as from renderDefault function?

<?php

class SearchPresenter extends BasePresenter
{
    public function renderDefault() {
	if($this->getParam('query')) {
	    $this->search($this->getParam('query'));
	}
    }

    ....

    public function searchFormSucceeded($form)
    {
        $query = $form['query']->getValue();
        $this->search($this->getParam('query'));
    }

    private function search($query) {
	$itemIds = $this->searchService->findForUser($this->currentUser, $query);

        $this->template->items = array();
        if (!empty($itemIds)) {
            $this->template->items = $this->itemService->findById($itemIds);
        }
    }
}
?>

Last edited by Oggy (2014-03-12 21:37)

dimkalinux
Member | 24
+
0
-

Yes, im use this method with separated method for search but maybe exists better method?
Will be great use searchFormSucceeded() but i can not call it from renderDefault() becouse $form is not submited.

sKopheK
Member | 207
+
0
-

As Oggy suggested, call search() method and give it search query string as a parameter. In your case something like this will do

$this->search($this->getParameter('query'));
dimkalinux
Member | 24
+
0
-

In this case i can not use form validation. Im prefer keep all validation logic (like query length) in form.