How to submit form? (Callback is not executed)

Notice: This thread is very old.
wdolek
Member | 331
+
0
-

I have troubles with creating login form. I looked into examples, made similar thingy, but it doesn't work – callback is not executed, and can't figure out why. (I'm new to 2.0)

presenter:

public function loginFormSubmitted (Form $form)
{
    $conf = $this->getContext()->params;
    if (!isset($conf['admin']['username'], $conf['admin']['password'])) {
        $this->flashMessage('uživatelské jméno nebo heslo nejsou nastaveny', 'error');
        $this->redirect('Admin:loginForm');
    }

    try {
        $this->getUser()->login(
            $form['in_username']->getValue(),
            $form['in_password']->getValue()
        );
        $this->redirect('Admin:default');
    } catch (Nette\Security\AuthenticationException $e) {
        $this->flashMessage('neplatné přihlašovací údaje', 'error');
        $this->redirect('Admin:loginForm');
    }
}

public function createComponentLoginForm ()
{
    $form = new Form();

    $form->addText('in_username', 'uživatel')
        ->setRequired('zadejte uživatelské jméno');

    $form->addPassword('in_password', 'heslo')
        ->setRequired('zadejte heslo');

    $form->addSubmit('btn_ok', 'Ok');

    $form->onSuccess[] = new Nette\Callback($this, 'loginFormSubmitted');
    return $form;
}

template:

{control loginForm}

I also tried callback() function, or assign array() as in old Nette. Also tried onSubmit but no luck. What am I missing?

Also I noticed, that form HTML is:

<form action="" method="post" id="frm-">

And another thing, which looks strange – despite I defined message for filling form input (in czech), I get default message in English… bit confusing.

Nette Framework 2.0-beta (revision f38d86f released on 2011–08–24)

voda
Member | 561
+
0
-

Maybe you are using Nette\Forms\Form instead of Nette\Application\UI\Form.

HosipLan
Moderator | 4668
+
0
-

Authenticating doesn't work that way..

class AdminAuthenticator extends Nette\object implements Nette\Security\IAuthenticator
{
	private $adminCredentials = array();

	public function __construct(array $adminCredentials)
	{
		$this->adminCredentials = $adminCredentials;
	}

	public function authenticate(array $credentials)
	{
		if (!$this->usernameEquals($credentials[self::USERNAME])) {
			throw new Nette\Security\AuthenticationException;
		}

		if (!$this->passwordEquals($credentials[self::PASSWORD])) {
			throw new Nette\Security\AuthenticationException;
		}

		return new Nette\Security\Identity('admin', // ID
			array('admin'), // role
			array() // data accessible throught $identity->key
		);
	}

	private function usernameEquals($username)
	{
		return $username === $this->adminCredentials['username'];
	}

	private function passwordEquals($password)
	{
		return $this->hashPassword($password) === $this->adminCredentials['password'];
	}

	private function hashPassword($password)
	{
		return sha1($password);
	}
}

I got a little carried away … But the class should be obviouse, now usage:

You set this class as authenticator in config

services:
	authenticator:
		class: AdminAuthenticator
		arguments: ['%admin%']

Now you can authenticate your user

public function loginFormSubmitted (Form $form)
{
    try {
        $this->getUser()->login(
            $form['in_username']->getValue(),
            $form['in_password']->getValue()
        );
        $this->redirect('Admin:default');

    } catch (Nette\Security\AuthenticationException $e) {
        $this->flashMessage('neplatné přihlašovací údaje', 'error');
        $this->redirect('Admin:loginForm');
    }
}
wdolek
Member | 331
+
0
-

voda wrote:

Maybe you are using Nette\Forms\Form instead of Nette\Application\UI\Form.

(facepalm) … ok, this was it :) thanks!

HosipLan wrote:

I'm making wth-arci-simple authentication :) I think I got it, but don't need this now ;) (checking of conf for credentials in presenter is there just for somebody, who will come to page after me).

Of course I have proper Authenticator class implementation with checking it again…


And BTW, I was looking for class AppForm %)

Last edited by wdolek (2011-10-19 22:13)