Form and dynamic select content

Notice: This thread is very old.
pistols
Member | 26
+
0
-

Hi there,

I have a form, which must change his select values depending on the current request

<?php
	public function createComponentAddUnitForm()
	{
		$form = new Form();

		$form->addSelect("units_types_id", "unit:")
			 ->setPrompt('choose the unit type');

		$form->addText('serialnumber', 'S/N:')
		->setAttribute('placeholder', '23487920');

		$form->addSubmit('send', 'Save');

		$form->onSuccess[] = $this->addUnitFormSucceeded;

		return $form;
	}
?>

so if my request is

http://domain/showForm/15

I need to fill the units_types_id with a different set of values then if the request comes from

http://domain/showForm/16

I tried to do something like this

in my latte file:

<?php
{control addUnitForm, $group->id}
?>

and my component function

<?php
public function createComponentAddUnitForm($id)
	{
		$form = new Form();
		$selectValues = $this->storage->getById($id)->fetchPairs();
		$form->addSelect("units_types_id", "unit:", $selectValues)
			 ->setPrompt('choose the unit type');

		$form->addText('serialnumber', 'S/N:')
		->setAttribute('placeholder', '23487920');

		$form->addSubmit('send', 'Save');

		$form->onSuccess[] = $this->addUnitFormSucceeded;

		return $form;
	}
?>

But it fails silently. It doesn't show a form at all…

Could you please guide me, how i can pass a parameter to the createComponent<name>

Just for the record i solved it like this:

<?php
public function createComponentAddUnitForm($id)
	{
		$form = new Form();
		$group = (int)$this->getParameter("groupId");
	        ....
?>

but Yes, I am not proud of it :)

Last edited by pistols (2014-04-16 13:43)

jiri.pudil
Nette Blogger | 1029
+
0
-

You don't need to pass anything into the component. You can modify it from outside, e.g. in the action* method:

$this['addUnitForm']['units_types_id']->setItems($selectValues);
pistols
Member | 26
+
0
-

Yees!, thanks a lot, your code is quite more cleaner. I thought there is a way, but i couldn't find it in the docs. (the setItems method i mean…)

Cheers