Jak zachytit výběr v $form->addSelect a spustit podprogram s vybranou hodnotou?

Karel Chramosil
Member | 105
+
0
-

Dobrý den,
(nette 3.1, PHP 7.4) mám aplikaci kde by se mi líbilo zachytít výběr v $form->addSelect a spustit podprogram, který vyplní zbylá souvisejídí políčka vybranými daty.

            $form->addSelect('nazev_spotrebice', 'Název spotřebiče:', $this->database->table('spotrebic')
                ->order('nazev_spotrebice ASC')
                ->fetchPairs('id', 'nazev_spotrebice'));
                //->setPrompt('NEVYBRÁNO:');
            $form->addText('nazev_spotrebice_novy', 'Název spotřebiče: (Zadejte, pokud nevybrán výše):', 50, 200)
				->setDefaultValue($spotrebics->nazev_spotrebice);
            $form->addText('vyrobcislo', 'Výrobní číslo: (Po uložení nelze měnit!!!):', 50, 100)
                ->addRule(Form::FILLED, 'Je nutné zadat výrobní číslo.')
                ->addRule([$this,'vyrobCisloExists'], 'Vyrobní číslo již existuje!')
				->setDefaultValue($spotrebics->vyrobcislo);

po výběru názvu spotřebiče bych chtěl vypnit textová pole pokud nebudou v DB prázná.
Hledal jsem v dokumentaci, ale nedokázal jsem najít řešení.

Děkuji za jakýkoliv nápad.

Karel Chramosil

Rick Strafy
Nette Blogger | 63
+
0
-

Hi, could you please edit your message and translate to english? This is the en version of the forum.

I would try with encapsulating form into snippet, add onchange-submit to the select and after form is sent with misssing data, snippet should be redrawn, not that form will have info about what was selected, or you can do it with javascript, but that's a little bit complicated, since you need another endpoint with the data and it's more js-based than nette based, but that may cause validation problem since you may add some select options that weren't initially in the form itself, that's why I would use snippet.

Last edited by Rick Strafy (2022-01-09 17:40)

Karel Chramosil
Member | 105
+
0
-

Good day,
(net 3.1, PHP 7.4) I have an application where I would like to capture the selection in $ form→ addSelect and run a subroutine that fills in the remaining related data.

            $form->addSelect('nazev_spotrebice', 'Název spotřebiče:', $this->database->table('spotrebic')
                ->order('nazev_spotrebice ASC')
                ->fetchPairs('id', 'nazev_spotrebice'));
                //->setPrompt('NEVYBRÁNO:');
            $form->addText('nazev_spotrebice_novy', 'Název spotřebiče: (Zadejte, pokud nevybrán výše):', 50, 200)
				->setDefaultValue($spotrebics->nazev_spotrebice);
            $form->addText('vyrobcislo', 'Výrobní číslo: (Po uložení nelze měnit!!!):', 50, 100)
                ->addRule(Form::FILLED, 'Je nutné zadat výrobní číslo.')
                ->addRule([$this,'vyrobCisloExists'], 'Vyrobní číslo již existuje!')
				->setDefaultValue($spotrebics->vyrobcislo);

after selecting the appliance name, I would like to turn off the text box if they are not empty in the DB.
I searched the documentation, but I can't find a solution.

Thank you for any idea.

Karel Chramosil

Just run me after choosing
$ form→ onSuccess [] = [$ this, ‘editFormSubmitted’];

Could you elaborate on me: “I would try with encapsulating form into snippet, add onchange-submit to the select and after form is sent with misssing data,”

Thank you

Rick Strafy
Nette Blogger | 63
+
0
-

Add form into {snippet test}{/snippet} macro in latte, make last 2 fields unnecessary to submit the form, when the form is submitted, check if last 2 fields are null, and if they are, save the state in presenter (some variable in presenter), call (in presenter) $this->redrawControl(‘test’) and form will be rendered again, in the form rendering (that's where $form->add.. methhods are) you only check what the state was, since it's the same request and you should have runtime variables in presenter.

Karel Chramosil
Member | 105
+
0
-

Thank you for the advice,

but basically I only need to call the

 function FormSubmitted (Form $ form)

automatically after selecting in

 $form->addSelect('firma_vyrobce', 'Vybrat firmu výrobce:(po výběru uložte data):', $this->database->table('firma_vyrobce')
                    ->order('firma ASC')
                    ->fetchPairs('id', 'firma'))
                    ->setPrompt('AAA nevybráno');

Thank you for the advice.

Karel Chramosil