Retrieve data from addselect

Notice: This thread is very old.

5 years ago

kevchal
Member | 10
+
0
-

hello,
I tried to retrieve data from a Form. It worked for addText input but not for the fields addSelect.
here is my code.

<?php
protected function createComponentDateForm() {
		$form = new \Nette\Application\UI\Form();
		$form->addText('from', 'From')->setDefaultValue($this->startDate->format('Y-m-d'));
		$form->addText('to', 'To')->setDefaultValue($this->endDate->format('Y-m-d'));
		$form->addSelect('owner', 'Owner', $this->owner)->setAttribute('onchange', 'submit()');
		$form->addSelect('apartment', 'Apartment', $this->apartment)->setAttribute('onchange', 'submit()');
		$form->addSubmit('save');
		$form->onSuccess[] = array($this, 'dateFormSubmitted');
		return $form;
	}

public function dateFormSubmitted(\Nette\Application\UI\Form $form) {
		$values = $form->getHttpData($form::DATA_TEXT | $form::DATA_KEYS);
		d($values,'values');
		$this->startDate = new \DibiDateTime($values['from']);
		$this->endDate = new \DibiDateTime($values['to']);
		$this->apartment = $values['apartment'];
		$this->owner = $values['owner'];
		$this->redirect('this', array('from' => $values['from'], 'to' => $values['to'], 'owner' => $values['owner'], 'apartment' => $values['apartment']));
	}
?>

But, I would like to retrieve the String inside those addSelect and not the position of the item selected inside the addselect

I dumped the values of the submited form and I have

<?php
from => "2015-03-31" (10)
to => "2015-03-31" (10)
owner => "6"
apartment => "0"
?>

Am I doing it wrong ???

Thanks for your help

5 years ago

frocco
Member | 46
+
0
-

how are you loading the owner and appartment?

5 years ago

kevchal
Member | 10
+
0
-

Thanks for your answer frocco,
Inside the presenter I call to the method

<?php
$this->apartment = $this->models->stats->getArrayPropertyName();
?>

the method inside my model which feed apartment is :

<?php
function getPropertyName()
	{
		return $this->db()->fetchAll("SELECT DISTINCT a.name FROM apartments a WHERE a.name IS NOT NULL");
	}

	function getArrayPropertyName(){
		$result = array();
		$i = 0;
		$owners = $this->getPropertyName();
		foreach($owners as $owner){
			$result[$i] = $owner['name'];
			$i++;
		}
		return $result;
	}
?>

I do exactly the same thing for owner the only difference is the name of the method used and the query on the data base

5 years ago

David Matějka
Moderator | 6202
+
0
-

In which method do you call getArrayPropertyName()?

5 years ago

frocco
Member | 46
+
0
-
try $result['name'] = $owner['name'];

5 years ago

kevchal
Member | 10
+
0
-

frocco

<?php
$result['name'] = $owner['name'];
?>

with that now when I dump the value returned by my form I have :

<?php
from => "2015-03-31" (10)
to => "2015-03-31" (10)
owner => "11" (2)
apartment => "name" (4)
?>

I use it in RenderDefault() method David

5 years ago

David Matějka
Moderator | 6202
+
0
-

@kevchal move it to the actionDefault method.
After submitting, form is processed before render* method (and after the action*) therefore $this->owner is empty while the form is created for the processing. In Nette, form must be created exactly the same in the both rendering and processing phase (security reasons – nobody can send you faked values)

5 years ago

frocco
Member | 46
+
0
-
$result[$owner['name']] = $owner['name'];

5 years ago

kevchal
Member | 10
+
0
-

Thank you for your help now it works :-)

5 years ago

frocco
Member | 46
+
0
-

Your welcome. Glad to help.