Retrieve data from addselect
- kevchal
- Member | 10
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
- kevchal
- Member | 10
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
- David Matějka
- Moderator | 6445
@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)