How to use flash messages in reusable forms?

Notice: This thread is very old.
netteman
Member | 125
+
0
-

Hi

I created a reusable form https://doc.nette.org/en/forms#… and I have the form definition and processing in the same place (nice!)

I'd also like to use flash messages in the processing part – is it possible or not?

Thx :)

GEpic
Member | 566
+
+2
-

netteman wrote:

Hi

I created a reusable form https://doc.nette.org/en/forms#… and I have the form definition and processing in the same place (nice!)

I'd also like to use flash messages in the processing part – is it possible or not?

Thx :)

Hello, flashMessages are part of Presenter, so if your forms (components) are attached to some, you can use this inside the component – $this->getPresenter()->flashMessage();

Last edited by GEpic (2016-08-25 21:04)

Martk
Member | 661
+
+1
-

You can create component for flash messages and inject it to form create:

component with flashes

interface IFlashMessages {
	public function flashMessage($message, $type = 'info');
}

class FlashMessages implements IFlashMessages {

	private $application;

	public function __construct(Nette\Application\Application $application) {
		$this->application = $application;
	}

	public function flashMessage($message, $type = 'info') {
		if (!$this->application->getPresenter()) {
			throw new Exception('Presenter is not created yet.');
		}

		return $this->application->getPresenter()->flashMessage($message, $type);
	}

}

and inject it:

use Nette\Application\UI\Form;

class SignInFormFactory {

	private $flashes;

	public function __construct(IFlashMessages $flashes) {
		$this->flashes = $flashes;
	}

    /**
     * @return Form
     */
    public function create()
    {
        $form = new Form;

        $form->addText('name', 'Name:');
        // ...
        $form->addSubmit('login', 'Log in');

		$form->onSuccess[] = function () {
			$this->flashes->flashMessage('...');
		};

        return $form;
    }
}

Last edited by Antik (2016-08-25 22:15)

netteman
Member | 125
+
0
-

Thanks :)