Projekt

Obecné

Profil

« Předchozí | Další » 

Revize e19aef4e

Přidáno uživatelem Filip Jani před více než 5 roky(ů)

Re #7503 přidání knihovny pro dynamický formulář
+ vytvoření ukázkového formuláře a načítání jeho defaultních hodnot
+ latte šablona pro prvky formuláře

Zobrazit rozdíly:

app/AdminModule/component/Example/ExampleDynamic.latte
1
{form form}
2
    <div class="row mb-2">
3
        <div class="col-1">
4
            {label element}
5
        </div>
6
        <div class="col-9">
7
            {input element}
8
        </div>
9
    </div>
10

  
11
    <div n:multiplier="books">
12
        <div class="row mb-2">
13
            <div class="col-1"><label n:name="line">Řádka</label></div>
14
            <div class="col-3"><input n:name="line" n:class="form-control" required></div>
15
            <div class="col-1 offset-1"><label n:name="book">Knížka</label></div>
16
            <div class="col-3"><input n:name="book" n:class="form-control"></div>
17
            <div class="col-2">{btnRemove 'class' => 'btn btn-danger'}</div>
18
        </div>
19
    </div>
20

  
21
    {btnCreate books class => 'btn btn-success'}
22

  
23
    <div class="row mt-5">
24
        <div class="col-2">
25
            {input submit}
26
        </div>
27
    </div>
28

  
29
    <hr class="mt-5">
30
    <h4>Výsledky</h4>
31

  
32
    <code>
33
        {snippet results}
34
            {ifset $results}
35
                {foreach $results as $element => $value}
36
                    {if $value instanceof \Nette\Utils\ArrayHash}
37
                        {foreach $value as $el => $val}
38
                            <b>Element:</b> {$element}[{$el}], <b>value:</b> {var_dump($val)}
39
                            <br>
40
                        {/foreach}
41
                    {else}
42
                        <b>Element:</b> {$element}, <b>value:</b> {$value}<br>
43
                    {/if}
44
                {/foreach}
45
            {/ifset}
46
        {/snippet}
47
    </code>
48
{/form}
app/AdminModule/component/Example/ExampleDynamic.php
1
<?php
2

  
3

  
4
namespace App\AdminModule\Components;
5

  
6

  
7
use App\Utils\Form;
8
use Nette\Application\UI\Control;
9
use Nette\Forms\Container;
10

  
11
/**
12
 * Ukázkový dynamický formulář - SMAZAT PŘI PŘEDÁNÍ!
13
 *
14
 * @package App\AdminModule\Components
15
 */
16
class ExampleDynamic extends Control
17
{
18
    public function render()
19
    {
20
        $this->template->setFile(__DIR__ . '/ExampleDynamic.latte');
21
        $this->template->render();
22
    }
23

  
24
    public function createComponentForm()
25
    {
26
        $form = new Form;
27

  
28
        $min = 1; // Minimální počet kopií které se zobrazí ve formuláři (tj. 1 = minimálně jednou tam budou ty prvky)
29

  
30
        $form->addText('element', 'Další prvky');
31

  
32
        // Definice dynamických prvků
33
        $multiplier = $form->addMultiplier('books', function (Container $container)
34
        {
35
            $container->addText('line', 'Řádka');
36
            $container->addText('book', 'Knížka');
37
        }, $min);
38

  
39
        // Definice tlačítek pro přidání / odebrání řádku
40
        $multiplier->addCreateButton('Add')->addClass('btn btn-primary');
41
        $multiplier->addRemoveButton('Remove')->addClass('btn btn-danger');
42

  
43
        $form->setDefaults($this->getContainerDefaults());
44

  
45
        $form->addSubmit('submit', 'Odeslat');
46
        $form->onSuccess[] = [$this, 'formSuccess'];
47

  
48
        return $form;
49
    }
50

  
51
    public function formSuccess(Form $form)
52
    {
53
        \Tracy\Debugger::barDump($form->getValues());
54
        $this->template->results = $form->getValues();
55
        $this->redrawControl('results');
56
    }
57

  
58
    /**
59
     * Vrací testovací pole s defaultníma hodnotama pro ukázku jak se plní formulář při editaci
60
     */
61
    public function getContainerDefaults()
62
    {
63
        return [
64
            'element' => 'Nějaký další prvky',
65
            'books' =>  // název kontaineru v addMultiplier
66
                [
67
                    ['line' => '1.', 'book' => 'kniha 1.'],
68
                    ['line' => '2.', 'book' => 'kniha 2.'],
69
                    ['line' => '3.', 'book' => 'kniha 3.']
70
                ]
71
        ];
72
    }
73

  
74
}
75

  
76
interface IExampleDynamicFactory
77
{
78
    /**
79
     * @return ExampleDynamic
80
     */
81
    public function create();
82
}
app/AdminModule/presenters/ExamplePresenter.php
1
<?php
2

  
3

  
4
namespace App\AdminModule\Presenters;
5

  
6

  
7
use App\AdminModule\Components\IExampleDynamicFactory;
8

  
9
class ExamplePresenter extends BaseAdminPresenter
10
{
11
    /**
12
     * @var IExampleDynamicFactory
13
     */
14
    private $dynamicFactory;
15

  
16
    public function __construct(IExampleDynamicFactory $dynamicFactory)
17
    {
18

  
19
        $this->dynamicFactory = $dynamicFactory;
20
    }
21

  
22
    public function createComponentForm()
23
    {
24
        return $this->dynamicFactory->create();
25
    }
26
}
app/AdminModule/templates/Example/default.latte
1
{block content}
2
{control form}
app/config/components.neon
22 22
    - App\AdminModule\Components\IBookEditFormFactory
23 23

  
24 24
    - App\FrontModule\Components\ITransliterationSearchResultListFactory
25

  
26
    # Example - smazat při předání!
27
    - App\AdminModule\Components\IExampleDynamicFactory
app/config/config.neon
14 14
services:
15 15
	router: App\RouterFactory::createRouter
16 16
	authenticator: App\Model\Authenticator
17

  
18
extensions:
19
    replicator: WebChemistry\Forms\Controls\DI\MultiplierExtension
composer.json
20 20
		"nette/utils": "^2.4",
21 21
		"latte/latte": "^2.4",
22 22
		"tracy/tracy": "^2.4",
23
		"ublaboo/datagrid": "^5.6"
23
		"ublaboo/datagrid": "^5.6",
24
		"contributte/forms-multiplier": "^3.0"
24 25
	},
25 26
	"require-dev": {
26 27
		"nette/tester": "^2.0"
composer.lock
4 4
        "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
5 5
        "This file is @generated automatically"
6 6
    ],
7
    "content-hash": "6018197517a97474da6c1a7c8e26f4fc",
7
    "content-hash": "cb338bcaa5ecca136cb052ce84ef77ac",
8 8
    "packages": [
9
        {
10
            "name": "contributte/forms-multiplier",
11
            "version": "v3.0.6",
12
            "source": {
13
                "type": "git",
14
                "url": "https://github.com/contributte/forms-multiplier.git",
15
                "reference": "c76e4dfb298174bbe0fcdf3b22130271236f69f6"
16
            },
17
            "dist": {
18
                "type": "zip",
19
                "url": "https://api.github.com/repos/contributte/forms-multiplier/zipball/c76e4dfb298174bbe0fcdf3b22130271236f69f6",
20
                "reference": "c76e4dfb298174bbe0fcdf3b22130271236f69f6",
21
                "shasum": ""
22
            },
23
            "require": {
24
                "nette/forms": "^2.4",
25
                "php": ">= 5.6"
26
            },
27
            "require-dev": {
28
                "codeception/codeception": "^2.4",
29
                "latte/latte": "^2.3",
30
                "nette/application": "^2.3",
31
                "nette/di": "^2.3",
32
                "phpunit/phpunit": "~5.0 || ~6.0 || ~7.1.0",
33
                "webchemistry/testing-helpers": "~1.1.2"
34
            },
35
            "type": "library",
36
            "autoload": {
37
                "psr-4": {
38
                    "WebChemistry\\Forms\\Controls\\": [
39
                        "src/"
40
                    ]
41
                }
42
            },
43
            "notification-url": "https://packagist.org/downloads/",
44
            "description": "Multiplier for nette forms",
45
            "keywords": [
46
                "Forms",
47
                "multiplier",
48
                "nette",
49
                "webchemistry"
50
            ],
51
            "time": "2019-03-05T08:14:12+00:00"
52
        },
9 53
        {
10 54
            "name": "latte/latte",
11 55
            "version": "v2.4.8",

Také k dispozici: Unified diff