Search form gives Component '' is not attached to ‘Nette\Application\UI\Presenter’

about a year ago

rolandd
Member | 3
+
0
-

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:

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.

about a year ago

CZechBoY
Member | 3452
+
0
-

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();
	}
}

about a year ago

rolandd
Member | 3
+
0
-

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?

about a year ago

CZechBoY
Member | 3452
+
0
-

Component si rendered in latte using {control} macro. In your case in file templates/Rules/default.latte (or sth similar) you should add {control rulesForm} somewhere.

about a year ago

rolandd
Member | 3
+
+1
-

@CZechBoY Thank you so much for your patience with me. That was the missing link, adding the {control rulesForm} in my Presenter file made the search form show up. Now I can continue learning Nette. Have a great weekend.