Search form gives Component '' is not attached to ‘Nette\Application\UI\Presenter’
- rolandd
- Member | 3
Hello,
A newcomer to Nette here. I am trying to implement a search form, this form
is used across multiple presenters but as the title says I get the error
Component '' is not attached to 'Nette\Application\UI\Presenter'
.
I have followed the following pages:
- https://doc.nette.org/en/forms#…
- https://forum.nette.org/…-doing-wrong
- https://doc.nette.org/…s/form-reuse
What I have eventually implemented is this.
app/forms/SearchFormFactory.php
<?php
namespace App\Forms;
use Nette\Application\UI\Form;
class SearchFormFactory
{
public function create()
{
$form = new Form();
$form->addText('search', 'Search');
$form->addSubmit('send', 'Search');
return $form;
}
}
app/presenters/RulesPresenter.php
<?php
namespace App\Presenters;
use App\Forms\SearchFormFactory;
use App\Model\RuleManager;
class RulesPresenter extends BasePresenter
{
private $ruleManager;
public function __construct(RuleManager $ruleManager)
{
parent::__construct();
$this->ruleManager = $ruleManager;
}
public function renderDefault()
{
$this->template->search = (new SearchFormFactory)->create();
$this->template->rules = $this->ruleManager->getRules();
}
}
config.local.neon
services:
- App\Forms\SearchFormFactory
This generates the given error, when I remove the link to the
SearchFormFactory
, the page loads fine.
Where o where am I going wrong?
Thank you for any insights.
- CZechBoY
- Member | 3608
There is no createComponentSignInForm
method, which creates the
component.
Can you send how you use the component?
Also you should use dependency injection instead of manually creating SignInFormFactory.
<?php
namespace App\Presenters;
use App\Forms\SearchFormFactory;
use App\Model\RuleManager;
class RulesPresenter extends BasePresenter
{
private $ruleManager;
private $searchFormFactory;
public function __construct(RuleManager $ruleManager, SearchFormFactory $searchFormFactory $searchFormFactory)
{
parent::__construct();
$this->ruleManager = $ruleManager;
$this->searchFormFactory = $searchFormFactory;
}
public function renderDefault()
{
$this->template->rules = $this->ruleManager->getRules();
}
protected function createComponentSignInForm()
{
return $this->searchFormFactory->create();
}
}
- rolandd
- Member | 3
Hello @CZechBoY
Thank you for your quick reply and apologies for my late response. Life got in the way :)
So I have been following your pointers and here is what I have now. The searchFormFactory and translator are set in the parent. This gives me no error but I don't see the searchFormFactory form in the output either.
code
<?php
namespace App\Presenters;
use App\Forms\SearchFormFactory;
use App\Model\RuleManager;
use Kdyby\Translation\Translator;
class RulesPresenter extends BasePresenter
{
private $ruleManager;
/**
* Constructor.
*
* @param RuleManager $ruleManager
* @param Translator $translator
* @param SearchFormFactory $searchFormFactory
*
* @since 8.0.0
*/
public function __construct(RuleManager $ruleManager, Translator $translator, SearchFormFactory $searchFormFactory)
{
parent::__construct();
$this->ruleManager = $ruleManager;
$this->translator = $translator;
$this->searchFormFactory = $searchFormFactory;
}
/**
* Render the page.
*
* @return void
*
* @since 8.0.0
*/
public function renderDefault()
{
//$this->template->search = (new SearchFormFactory)->create();
$this->template->rules = $this->ruleManager->getRules();
}
public function createComponentRulesForm()
{
return $this->searchFormFactory->create();
}
}
?>
code
This is the neon file I am using.
{block content}
<table class="uk-table">
<thead>
<tr>
<th>{_rules.name}</th>
<th>{_rules.plugin}</th>
<th>{_rules.action}</th>
</tr>
</thead>
<tbody>
<tr n:foreach="$rules as $rule">
<td><a n:href="Rule:edit $rule->csvi_rule_id">{$rule->name}</a></td>
<td>{$rule->plugin}</td>
<td>{$rule->action}</td>
</tr>
</tbody>
</table>
You asked me to send you how I use this component. Not sure what you mean but here is what I plan to do. I want to show a list of rules and from this list the user can add or edit rules. Eventually these rules are used in the import and export of data. Before I get to those views, I must gather a lot more knowledge about Nette :)
Does this answer your question?