Formulář a zobrazení chybových hlášek

FilipStudený
Člen | 8
+
0
-

Zdravím, před pár dny jsem začal s Nette a potřeboval bych pomoci týkající se se zobrazováním chyb pro formuláře.

Co tím myslím, do listu <ul> v in.latte, bych chtěl zobrazit všechny chyby týkající se registrace, například když uživatel použije emailovou adresu, která je již použita, tak bych chtěl zobrazit chybovou hlášku „Emailová adresa je již obsazena“ nebo „Uživatelské jméno je použito“ nebo když uživatel klikne na tlačítko registrace, tak aby se zobrazila varovná hláška „Vyplňte všechna pole“ atd..

Zde je můj AuthPresenter a in.latte

namespace App\Presenters;

use App\CustomAuthenticator;
use Nette\Application\UI\Form;
use Nette\Application\UI\Presenter;
use Nette;


class AuthPresenter extends Presenter
{
    public function __construct(private Nette\Database\Explorer $database, public CustomAuthenticator $authenticator)
    {}
//<div n:foreach="$flashes as $flash" n:class="flash, $flash->type">{$flash->message}</div>

    public function createComponentSignInForm(): Form
    {
        $form = new Form();

        $form->addText('username', 'Username:')->addError("Username can't be empty");
        $form->addEmail('email', 'Email:');
        $form->addPassword('password', 'Password:');
        $form->addPassword('repeat_password', 'Repeat password:')
            ->addRule(Form::EQUAL, 'Passwords do not match.', $form['password']);
        $form->addText('firstname', 'First name:');
        $form->addText('lastname', 'Last name:');
        $form->addSelect('gender', 'Gender:', ['male' => 'Male', 'female' => 'Female']);
        $form->addSubmit('submit', 'Register');
        $form->onSuccess[] = function (Form $form, \stdClass $values) {
            try {
                $this->authenticator->createNewUser((array) $values);
                $this->flashMessage('User successfully registered.', 'success');
                $this->redirect('Home:');
            } catch (\Exception $e) {
                $this->flashMessage($e->getMessage(), 'danger');
            }
        };
        return $form;
    }

    public function createComponentLoginForm(): Form{
        $form = new Form();
        $form->addText('email')->setRequired();
        $form->addText('password')->setRequired();
        $form->addText('submit');
        $form->onSuccess[] = [$this, 'loginUser'];
        return $form;
    }

    public function loginUser(array $data): void{
        try {
            $this->getUser()->setExpiration('14 days'); // set the expiration time
            $this->getUser()->login($data['email'], $data['password']); // call the CustomAuthenticator
        } catch (Nette\Security\AuthenticationException $e) {
            $this->flashMessage($e->getMessage(), 'danger');
        }
    }

    public function actionOut(): void{
        $this->getUser()->logout();
        $this->redirect('Home:');
    }

}

A zde je můj registrační formulář:

{block content}
<div class="w-full md:w-1/2 flex flex-col items-center justify-center m-4 shadow-2xl p-4">
    <div class="flex flex-row justify-evenly items-center">
        <img src="{$basePath}/images/kiwi_logo.svg" width="50" height="50" alt="user image" class="mr-4">
        <h2 class="font-bold text-2xl">Create new profile</h2>
    </div>

    <form n:name="signInForm" class="w-full">
        <div class="mb-4">
            <label for="username" class="block text-gray-700 text-sm font-bold mb-2">Username:</label>
            <input type="text" id="username" n:name="username" class="w-full border border-gray-300 p-2 rounded" required>
        </div>

        <div class="mb-4">
            <label for="email" class="block text-gray-700 text-sm font-bold mb-2">Email:</label>
            <input type="email" id="email" n:name="email" class="w-full border border-gray-300 p-2 rounded" required>
        </div>

        <div class="flex flex-row items-center justify-between">
            <div class="mb-4 mr-2 w-1/2">
                <label for="password" class="block text-gray-700 text-sm font-bold mb-2">Password:</label>
                <input type="password" id="password" n:name="password" class="w-full border border-gray-300 p-2 rounded" required>
            </div>

            <div class="mb-4 w-1/2">
                <label for="repeat_password" class="block text-gray-700 text-sm font-bold mb-2">Repeat password:</label>
                <input type="password" id="repeat_password" n:name="repeat_password" class="w-full border border-gray-300 p-2 rounded" required>
            </div>
        </div>

        <div class="mb-4">
            <label for="firstname" class="block text-gray-700 text-sm font-bold mb-2">First name:</label>
            <input type="text" id="firstname" n:name="firstname" class="w-full border border-gray-300 p-2 rounded" required>
        </div>

        <div class="mb-4">
            <label for="lastname" class="block text-gray-700 text-sm font-bold mb-2">Last name:</label>
            <input type="text" id="lastname" n:name="lastname" class="w-full border border-gray-300 p-2 rounded" required>
            <p class="h-auto w-full text-center text-xl bg-red-600 rounded">Last name can't be empty</p>
        </div>

        <div class="mb-4">
            <label for="gender" class="block text-gray-700 text-sm font-bold mb-2">Gender:</label>
            <select id="gender" n:name="gender" class="w-full border border-gray-300 p-2 rounded">
                <option class="text-xl text-center" value="male">Male</option>
                <option class="text-xl text-center" value="female">Female</option>
            </select>
        </div>

        <button n:name="submit" type="submit" class="float-right w-full md:w-auto mt-4 md:mt-0 text-white bg-gray-900 font-semibold py-2 px-4 rounded hover:bg-white hover:text-gray-900">Register</button>
    </form>

    <ul class="error-display">
		<li>Uživatelské jméno je již použito</li>
	</ul>
</div>
David Grudl
Nette Core | 8218
+
+1
-

Ukázku řešení máš tady https://github.com/…thentication, tohle je konkrétně vykreslení chyb