Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 62876c54

Přidáno uživatelem Filip Jani před téměř 6 roky(ů)

Re #7504 odstranění ukázkového dyn. formuláře a úprava formátování kódu

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/component/Transliteration/TransliterationEditForm.latte
87 87
    <div class="row">
88 88
        <div class="col-12">
89 89
            <h5>References</h5>
90
            <p>(series, number, page)</p>
90 91
            <div n:multiplier="references">
91 92
                <div class="row mb-2">
92 93

  
......
109 110
        </div>
110 111
    </div>
111 112

  
112
    <div class="row mt-2">
113
    <div class="row mt-5">
113 114
        <div class="col-4">
114 115
            {input submit}
115 116
        </div>
app/AdminModule/component/Transliteration/TransliterationEditForm.php
12 12
use App\Model\Repository\TransliterationRepository;
13 13
use App\Utils\Form;
14 14
use Nette\Application\UI\Control;
15
use Nette\Database\Table\ActiveRow;
15 16
use Nette\Forms\Container;
16 17

  
17 18
/**
......
60 61
    /**
61 62
     * TransliterationEditForm constructor.
62 63
     * @param TransliterationRepository $transliterationRepository
64
     * @param BookRepository $bookRepository
65
     * @param MuseumRepository $museumRepository
66
     * @param OriginRepository $originRepository
67
     * @param BookTypeRepository $bookTypeRepository
68
     * @param LitReferenceRepository $litReferenceRepository
63 69
     */
64
    public function __construct(TransliterationRepository $transliterationRepository, BookRepository $bookRepository,
65
                                MuseumRepository $museumRepository, OriginRepository $originRepository,
66
                                BookTypeRepository $bookTypeRepository, LitReferenceRepository $litReferenceRepository)
70
    public function __construct(TransliterationRepository $transliterationRepository,
71
                                BookRepository $bookRepository,
72
                                MuseumRepository $museumRepository,
73
                                OriginRepository $originRepository,
74
                                BookTypeRepository $bookTypeRepository,
75
                                LitReferenceRepository $litReferenceRepository
76
    )
67 77
    {
68 78
        parent::__construct();
69 79

  
......
154 164
    /**
155 165
     * Zkontroluje jestli není nějaká refence prázná
156 166
     *
157
     * @param $references pole referencí
167
     * @param $references array referencí
158 168
     * @return bool
159 169
     */
160 170
    public function isAnyReferenceEmpty($references)
......
173 183
    /**
174 184
     * Odstraní odebrané reference transliterace
175 185
     *
176
     * @param $newReferences pole nových transliterací
186
     * @param $newReferences array nových transliterací
177 187
     */
178 188
    public function deleteRemovedReferences($newReferences)
179 189
    {
......
192 202
    /**
193 203
     * Zkontroluje jestli je refence v poli referencí
194 204
     *
195
     * @param $reference reference ke kontrole
196
     * @param $newReferences pole referencí
205
     * @param $reference ActiveRow reference ke kontrole
206
     * @param $newReferences array pole referencí
197 207
     * @return bool
198 208
     */
199 209
    public function isRemovedReference($reference, $newReferences)
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
23 23
    - App\AdminModule\Components\ITransliterationEditFormFactory
24 24

  
25 25
    - App\FrontModule\Components\IKeyboard
26
    - App\FrontModule\Components\ITransliterationSearchResultListFactory
27

  
28
    # Example - smazat při předání!
29
    - App\AdminModule\Components\IExampleDynamicFactory
26
    - App\FrontModule\Components\ITransliterationSearchResultListFactory
app/model/repository/BookRepository.php
43 43
     *
44 44
     * @return array
45 45
     */
46
    public function getBookAbbrevForSelect(){
47
        return $this->findAll()->fetchPairs(BookRepository::COLUMN_ID,BookRepository::COLUMN_BOOK_ABREV);
46
    public function getBookAbbrevForSelect()
47
    {
48
        return $this->findAll()->fetchPairs(BookRepository::COLUMN_ID, BookRepository::COLUMN_BOOK_ABREV);
48 49
    }
49 50

  
50 51
}

Také k dispozici: Unified diff