PHP code example of geniv / nette-general-form

1. Go to this page and download the library: Download geniv/nette-general-form library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

geniv / nette-general-form example snippets


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

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;

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

class MyForm extends Control implements ITemplatePath

...

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

$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');
    }
};

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

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

    $message = $emailNotifyEvent->getMessage();
    $message->setFrom('[email protected]');

    $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('[email protected]');

    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 protected]');
            break;
    }
};

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

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
    }
}