PHP code example of mpospiech / doctrine-forms

1. Go to this page and download the library: Download mpospiech/doctrine-forms 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/ */

    

mpospiech / doctrine-forms example snippets


class FormPresenter extends \Nette\Application\UI\Presenter {
	/** @var BaseFormFactory @inject */
    public $baseFormFactory;
    
    /** @var DoctrineEntity */
    private $currentEntity; // pokud se ma jednat o update
    
    public function createComponentBaseForm() {
    	$component = $this->baseFormFactory->create(DoctrineEntity::class, ($this->currentEntity ? $this->currentEntity->id : 0));
        
        $self = $this;
        $component->addAfterSuccess(function() use ($self) {
        	$self->redirect('this');
        });
        
        $form = $component->getForm();
        
        $form->onSuccess[] = function(\Nette\Forms\Form $form, \Nette\Utils\ArrayHash $values) use ($self) {
        	if ($values->id) {
            	$self->flashMessage('Hodnoty formuláře byly úspěšně upraveny.', 'success');
            } else {
            	$self->flashMessage('Hodnoty z formuláře byly úspěšně uloženy.', 'success');
            }
        };
        
        return $form;
    }
}

class BaseFormFactory extends \mpospiech\Doctrine\Forms\FormFactory {
	public function setupForm(\Nette\Forms\Form $form) {
    	$form->addText('name', 'Název'); // pokud neni null v databazi, tak bude nastaven jako 

class BaseFormChangeValuesFactory extends \mpospiech\Doctrine\Forms\FormFactory {
	public function setupForm(\Nette\Forms\Form $form) {
    	$form->addText('name', 'Název'); // pokud neni null v databazi, tak bude nastaven jako ayHash $values) {
    	$values->date = \Nette\Utils\DateTime::createFromFormat('d.m.Y', $values->date); // do entity pro hodnotu date bude nyni odeslana instance DateTime
    }
}

class BaseFormWithTemplateFactory extends \mpospiech\Doctrine\Forms\FormFactory {
	public function __construct(\Kdyby\Doctrine\EntityManager $entityManager, \Nette\Application\UI\ITemplateFactory $templateFactory)
	{
		parent::__construct($entityManager, $templateFactory);

		$this->setTemplate(__DIR__ . '/baseForm.latte', ['variable' => 'valueVariable']);
	}

	public function setupForm(\Nette\Forms\Form $form) {
    	$form->addText('name', 'Název'); // pokud neni null v databazi, tak bude nastaven jako 

// vypnute uložení hodnoty do databáze
$form->addText('name', 'nameValue')
    ->setOption('autoSet', false);

// vypnuté nastavení výchozí hodnoty
$form->addText('name', 'nameValue')
    ->setOption('setDefaultValue', false);