FormContainer a setDefaults()
- edke
- Člen | 198
PHP Version 5.3.3–1ubuntu9.3
Nette 18b7f1639015e30c06de768780f028d09094909e
Snazim sa o naplnenie formulara, ktory pouziva FormContainer. Ako by mali vyzerat values, aby spravne bol naplneny aj container cez setDefaults() ?
$form = new \Nette\Application\AppForm($this, 'form');
$container = $form->addContainer('bookings');
foreach ($this->getSelectedBookings() as $booking) {
$container->addCheckbox($booking->getId(), $booking->getName());
}
..
$form->setDefaults($values);
Moj pokus vyzeral nejako takto, ale toto nefungovalo:
$bookings = new \Nette\ArrayHash();
foreach ($this->getSelectedBookings() as $booking) {
$bookings->{$booking->getId()} = true;
}
$values = new \Nette\ArrayHash();
$values->bookings = $bookings;
- Filip Procházka
- Moderator | 4668
public function actionDefault()
{
if (!$this['bookingsCosik']->isSubmitted()) {
$defaults = array();
foreach ($this->bookingsModel->selected as $booking) {
$defaults['bookings'][$booking->id] = TRUE;
}
$this['bookingsCosik']->setValues($defaults);
}
}
protected function createComponentBookingsCosik($name)
{
$form = new \Nette\Application\AppForm($this, $name);
$bookings = $form->addContainer('bookings');
foreach ($this->bookingsModel->selected as $booking) {
$bookings->addCheckbox($booking->id, $booking->name);
}
}
Nette\ArrayHash
nebo array
, tj fuk, mělo by to
fungovat stejně. Máš tam někde nějakou jinou chybu. Tohle musí
fungovat ;)
- edke
- Člen | 198
HosipLan wrote:
Nette\ArrayHash
neboarray
, tj fuk, mělo by to fungovat stejně. Máš tam někde nějakou jinou chybu. Tohle musí fungovat ;)
Tak som sa s tym teraz vyhral. Problemy robi
array_key_exists($name, $sub->cursor)
vo FormContainer->setValues(). V mojom pripade $sub->cursor je pole/ArrayHash pre dany container.
A teraz mozne situacie:
- ak $name (teda name checkboxu) je integer (tak ako v mojom pripade, je to id) a $sub->cursor je ArrayHash, test je false
- ak $name je integer a $sub->cursor je obycajny Array, test je true.
- ak $name je string a $sub->cursor je ArrayHash, test je true.
Teda staci na vybudovanie $defaults pouzit klasicke pole a problem nebude ziaden. Preco ale je problem s testom, ak key je integer (ale aj integer pretypovany na string) ? Bug or feature ?
Pripravil som maly testcase pre Phpunit (aj ako gist na github-e), ak by sa niekto nato chcel pozriet:
<?php
namespace Nette;
/**
* Test class for ArrayHash.
* Generated by PHPUnit on 2011-03-03 at 10:48:23.
*/
class ArrayHashTest extends \PHPUnit_Framework_TestCase {
/**
* @var ArrayHash
*/
protected $arrayHash;
/**
* @var array
*/
protected $array;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp() {
$this->array = Array('textKey' => true, '123' => true, 456 => true);
$this->arrayHash = ArrayHash::from($this->array);
}
public function testArrayKeyExists() {
foreach ($this->array as $key => $value) {
$this->assertTrue(\array_key_exists($key, $this->array));
}
}
public function testArrayHashKeyExists() {
foreach ($this->arrayHash as $key => $value) {
$this->assertTrue(\array_key_exists($key, $this->arrayHash));
}
}
}
- David Grudl
- Nette Core | 8228
Hmmm, tohle je buggy chování. Podporu objektů musím ještě trošku promyslet, zatím tam raději dávej pole.