geniv/nette-general-form

Form general class for Nette Framework

v1.1.6 2019-02-03 19:04 UTC

This package is auto-updated.

Last update: 2020-10-30 02:09:19 UTC


README

Installation

$ composer require geniv/nette-general-form

or

"geniv/nette-general-form": "^1.1"

require:

"php": ">=7.0",
"nette/application": ">=2.4",
"nette/component-model": ">=2.3",
"nette/di": ">=2.4",
"nette/mail": ">=2.4",
"nette/utils": ">=2.4",
"tracy/tracy": ">=2.4"

Include in application

usage IEvent:

class MyEvent implements IEvent

...

public function update(IEventContainer $eventContainer, array $values)

// usage method by IEventContainer
...
$eventContainer->getForm()
$eventContainer->addValues($values)
$eventContainer->setValues($values)
$eventContainer->removeValue($key)
$eventContainer->getComponent()
$eventContainer->getEventIndex()
$eventContainer->getEvents()

usage IFormContainer and IEventContainer (can use magic __invoke method):

private $formContainer;
private $eventContainer;
public $onSuccess, $onException;

public function __construct(IFormContainer $formContainer, array $events)

...

// $this->eventContainer = EventContainer::factory($this, $events, 'onSuccess', 'onException');
$this->eventContainer = EventContainer::factory($this, $events);
$this->formContainer = $formContainer;

...

$form->onSuccess[] = $this->eventContainer;

or the old way without __invoke:

try {
    $this->notify($form, $values);
    $this->onSuccess($values);
} catch (EventException $e) {
    $this->onException($e);
}

usage ITemplatePath (without return type!):

class MyForm extends Control implements ITemplatePath

...

public function setTemplatePath(string $path)
{
    $this->templatePath = $path;
}

Events for use (implements IEvent)

- DumpEvent
- FireLogEvent
- ClearFormEvent
- SetValueEvent     (setValues(array))
- CallbackEvent     (onCallback(IEventContainer, array))
- EmailNotifyEvent  (getMessage(), setTemplatePath(string))

SetValueEvent

- SetValueEvent([active: false, role: guest])

CallbackEvent

- CallbackEvent

usage in presenter:

$callbackEvent->onCallback[] = function (IEventContainer $eventContainer, array $value) {
    if ($this->identityModel->existLogin($value['login'])) {
        throw new EventException('duplicate login');
    }

    if ($this->identityModel->existEmail($value['email'])) {
        throw new EventException('duplicate email');
    }
};

EmailNotifyEvent

admin: Identity\Events\RegistrationEmailNotifyEvent     # email for admin
user: Identity\Events\RegistrationEmailNotifyEvent      # email for user
# or
- Identity\Events\ForgottenEmailNotifyEvent

where self class name (prevents multiple services in DI):

class RegistrationEmailNotifyEvent extends EmailNotifyEvent {}
class ForgottenEmailNotifyEvent extends EmailNotifyEvent {}

usage in presenter:

$emailNotifyEvent->onConfigure[] = function (IEventContainer $eventContainer, array $value) use ($emailNotifyEvent) {
    $emailNotifyEvent->setTemplatePath(__DIR__ . '/templates/Forgotten/emailFormForgotten.latte');

    $message = $emailNotifyEvent->getMessage();
    $message->setFrom('info@email.cz');

    $message->setSubject('informacni email pro uzivatele');
    $message->addTo($value['email']);
};

// or

$emailNotifyEvent->onConfigure[] = function (IEventContainer $eventContainer, array $value) use ($emailNotifyEvent) {
    $message = $emailNotifyEvent->getMessage();
    $message->setFrom('info@email.cz');

    switch ($eventContainer->getEventIndex()) {
        case 'user':
            $emailNotifyEvent->setTemplatePath(__DIR__ . '/templates/Registration/emailFormUser.latte');

            $message->setSubject('informacni email pro uzivatele');
            $message->addTo($value['email']);
            break;

        case 'admin':
            $emailNotifyEvent->setTemplatePath(__DIR__ . '/templates/Registration/emailFormAdmin.latte');

            $message->setSubject('informacni email pro admina');
            $message->addTo('email@email.com');
            break;
    }
};

event in definition is possible use several times, and define like anonymous index or text index

events:
    - DumpEvent
    - DumpEvent
    fire1: FireLogEvent
    fire2: FireLogEvent

Extension

usage GeneralForm:

$formContainer = GeneralForm::getDefinitionFormContainer($this);
$events = GeneralForm::getDefinitionEventContainer($this);

usage GeneralControl:

class MyForm extends GeneralControl {

    public function __construct(IFormContainer $formContainer, array $events, ITranslator $translator = null)
    {
        parent::__construct($formContainer, $events, $translator);

        $this->templatePath = __DIR__ . '/MyPath.latte';    // set path
    }
}

Exception

class: EventException