Add a new item bases on select previous

2 years ago

iwory
Member | 147
+
0
-

Hello,
in my form, I have a five dynamic buttons (I don't know names of than because are from the database).

After click on one of them, I need to generate data for next radio list control. Data are selected based on button name.

Cause:

  • In form, I have 5 buttons
  • I click on one of them
  • Form will submit by ajax
  • Form return the same form plus new control which I can select
  • I select one radio
  • Finish form

I tried to 3 ways, but always I have a problem to get a selected button name in create form.

Have you tried somebody it?
I will glad for a small help how to resolve it.

Thank you

// createComponent
...
foreach ($seasons as $season) {
    $months->addSubmit($season->getToken(), $season->getName())
        ->setValidationScope(false);
}
$clickedButtonValue = ???; // token
if($clickedButtonValue) {
    $months = $this->seasonModel->getMonthsBySeason($clickedButtonValue);
    $form->addRadioList('months', 'Month', $months);
}
...

2 years ago

rkor
Member | 62
+
0
-
$clickedButton = $form->isSubmitted();

https://api.nette.org/…ms.Form.html#…

2 years ago

iwory
Member | 147
+
0
-

I can not use it because I get an error Form is not anchored and therefore can not determine whether it was submitted.

2 years ago

duke
Member | 650
+
0
-

You need to anchor it manually. E.g.:

protected function createComponentFooForm()
{
    $form = new UI\Form;
    // TODO: add inputs (submit buttons), etc.
    $this->addComponent($form, 'fooForm'); // anchors the component to the component tree under name 'fooForm'
    if (!$form->isSubmitted()) {
        // TODO
    }

    return $form;
}

If you don't anchor it yourself, it will be automatically anchored after the createComponentFooForm method returns.

2 years ago

David Matějka
Moderator | 5934
+
0
-

Hi, it is a bit problematic. When you submit the final form, same fields (and options for the “months” radio) has to be defined like it was defined before submitting. But you cannot recreate “months” because you won't be able to detect previously clicked submit button.

Maybe you should reconsider the design – maybe include some redirect or use radio/select instead of multiple submit buttons.

2 years ago

iwory
Member | 147
+
0
-

Hi all, thanks for answers.
@DavidMatějka you're right, it will be a problem.

I resolved it with toggle and pre-generate of controls :)