Getting Forms right. Something is not working

Notice: This thread is very old.
davidevo
Member | 2
+
0
-

Hello all,
I have started in Nette Framework and it looks amazing, however there is something that is slipping and I cannot get it to work.

I am trying to create a form that will be shown in multiple presenters, a login form, so I am following the information shown here https://doc.nette.org/en/forms#…

I checked the app/forms and I saw a SignFormFactory.php so I decided to create one similar to it.

In my HomepagePresenter I use this code

<?php

namespace App\Presenters;

use Nette, Nette\Application\UI\Form;
use App\Model;
use App\Forms;

class HomepagePresenter extends BasePresenter
{
    private $database;

    public function __construct(Nette\Database\Context $database)
    {
        $this->database = $database;
    }

	public function renderDefault()
	{
		$this->template->users = $this->database->table('user')
        ->limit(5);
	}
    protected function createComponentLoginForm()
    {
        $form = new \App\Forms\LoginFormFactory();
        $form->create();
        return $form;
    }
}

And this is my LoginForm

<?php

namespace App\Forms;

use Nette;
use Nette\Application\UI\Form;


class LoginFormFactory
{

	/**
	 * @return Form
	 */
	public function create()
	{
		$form = new Form;
		$form->addText('username', 'Username:')
			->setRequired('Please enter your username.');

		$form->addPassword('password', 'Password:')
			->setRequired('Please enter your password.');

		$form->addSubmit('send', 'Sign in');

        $form->onSuccess[] = array($this, 'loginFormSucceeded');

		return $form;
	}
    public function loginFormSucceeded($form)
    {
        $values = $form->getValues();

        $user= $this->database->table('user');
        $user->where('user_name',$values->name);
        $user->where('user_password',sha1($values->password));


        if (!$user) {
            $this->error('User not found');
        }
        else{
            $this->flashMessage('Succesfully logged in '.$values->name, 'success');
            $this->template->$users_logged=$user;
        }
        $this->redirect('Homepage:default');
    }
}

I am getting the following error in the template when I call {control loginForm}

Method App\Presenters\HomepagePresenter::createComponentLoginForm() did not return or create the desired component.

I know I am doing something wrong, but I cannot see what it is.

Any help will be appreciated.

Jan Mikeš
Member | 771
+
+2
-

You are not returning instance of form nor saving it into variable $form that u are returning in createComponent method.

Correct solution:

protected function createComponentLoginForm()
    {
        $form = new \App\Forms\LoginFormFactory();
        return $form->create();
    }
Jan Mikeš
Member | 771
+
0
-

As well i would recommend you registering your form factories as services in config.neon and then using DI (@inject annotation etc) instead of creating factory by using $xx = new FormFactory()

This way you can not use benefits of factories – autopassing dependencies to your factory by constructor.

Nice explanation is here: https://doc.nette.org/…dependencies

davidevo
Member | 2
+
0
-

Thanks man.
I knew it was something stupid