Skip to content

Instantly share code, notes, and snippets.

@duskohu
Last active December 21, 2015 05:29
Show Gist options
  • Save duskohu/6257770 to your computer and use it in GitHub Desktop.
Save duskohu/6257770 to your computer and use it in GitHub Desktop.
Search Form Control for Nette framework
<?php
namespace Nas;
use Nette\Application\UI\Presenter;
use Nette\Application\UI\Form;
abstract class BasePresenter extends Presenter
{
/** @var ISearchFormControl */
private $searchFormControl;
/**
* INJECT SearchFormControl
* @param ISearchFormControl $searchFormControl
*/
public function injectSearchFormControl(ISearchFormControl $searchFormControl)
{
$this->searchFormControl = $searchFormControl;
}
/**
* CONTROL - SearchForm
* @return SearchFormControl
*/
public function createComponentSearchForm()
{
$control = $this->searchFormControl->create();
$control->setSearchPresenter('Search:default');
/** @var Form $form */
$form = $control['form'];
$form->addText('query', 'Hladať')
->setAttribute("placeholder", "Hladať");
return $control;
}
}
factories:
SearchFormControl:
create: \Nas\SearchFormControl
implement: \Nas\ISearchFormControl
<?php
namespace Nas;
use Nette\Application\UI\Control;
use Nette\Application\UI\Form;
/**
* Search form control
* @package Nas- Nette aplication System
* @author Dusan Hudak <[email protected]>
*/
class SearchFormControl extends Control
{
/** @var string */
private $searchPresenter = 'this';
/**
* @param $searchPresenter
* @return SearchFormControl provides fluent interface
*/
public function setSearchPresenter($searchPresenter)
{
$this->searchPresenter = $searchPresenter;
return $this;
}
/**
* FORM search
* @return Form
*/
protected function createComponentForm()
{
$form = new Form;
$elementPrototype = $form->getElementPrototype();
$elementPrototype->class[] = lcfirst($this->reflection->getShortName());
$form->addSubmit('submitSearch', 'Hladať');
$form->onSuccess[] = callback($this, 'processSubmit');
return $form;
}
/**
* PROCESS-SUBMIT-FORM
* Save new user
* @param Form $form
*/
public function processSubmit(Form $form)
{
$values = $form->getValues(TRUE);
$this->presenter->redirect($this->searchPresenter, $values);
}
/**
* RENDER
*/
public function render()
{
$template = $this->template;
$template->setFile(__DIR__ . '/' . $this->reflection->getShortName().'.latte');
$template->render();
}
}
/**
* ISearchFormControl
*/
interface ISearchFormControl
{
/** @return SearchFormControl */
function create();
}
<?php
namespace Nas;
class SearchPresenter extends BasePresenter
{
/** @persistent */
public $query;
public function actionDefault()
{
bd($this->query);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment