Replicator – addRemoveOnClick po invalidovani snippetu polozky containeru sa nevymazu
- duskohu
- Člen | 778
Ahojte, neviete mi niekto poradit? Mam Replicator a na addRemoveOnClick mam callback a v nom invalidujem snippet, ked dam odoslat formular a neprejde validaciou a nasledne na to dam odobrat polozku z containeru tak mi sice data polozky vymaze, ale polozka ostava, to znamena ked tam mam 4 polozky a dam jednu vymazat tak udaje sa vymazu ale samotnu polozku nevymaze takze zase ostanu 4, skusal som to aj klasicky (nie ajxom) a to funguje ok. Uz nejaky cas na tom sedim a nenapada ma vobec nic. Nevie niekto poradit?
$productItems = $form->addDynamic('productItems', function (Container $container) use ($_this) {
$container->addText("code")
->setAttribute("placeholder", "Kód produktu")
->addRule(Form::FILLED, 'Pole "Kód produktu" musí byť vyplnené!');
$container->addText("store")
->setAttribute("placeholder", "Ks")
->addRule(Form::FILLED, 'Pole "Skladom" musí byť vyplnené!')
->addRule(Form::FLOAT, 'Pole "Skladom" musí byť číselná hodnota!');
$container->addText("description")
->setAttribute("placeholder", "Popis");
$container->addHidden('id');
$container->addSubmit('remove', 'Odstrániť')
->setAttribute('class', 'ajax btn')
->addRemoveOnClick(callback($_this, 'deleteProductItem'));
}, 0);
$productItems->addSubmit('addProductItem', 'Pridať')
->setValidationScope(FALSE)
->addCreateOnClick(TRUE, function (\Kdyby\Replicator\Container $replicator, Container $container) use ($_this) {
if ($_this->presenter->isAjax()) {
$_this->invalidateControl('product-items');
}
});
$form->onValidate[] = callback($this, 'validateForm');
public function validateForm(\Nette\Application\UI\Form $form) {
$values = $form->getValues();
if (count($values['productItems']) <= 0) {
$form->addError('Musíte zadať aspoň jednu položku produktu.');
}
}
public function deleteProductItem(\Kdyby\Replicator\Container $replicator, Container $container) {
$this->invalidateControl('product-items');
}
<table n:snippet="product-items" >
<thead>
<tr>
<th width="150">Kód produktu</th>
<th width="50">Skladom</th>
<th>Popis</th>
<th width="70">{input productItems-addProductItem class => 'btn ajax'}</th>
</tr>
</thead>
<tbody>
{foreach $form["productItems"]->containers as $id => $container}
<tr>
<td>{$form->render($form['productItems'][$id]['code'])}</td>
<td>{$form->render($form['productItems'][$id]['store'])}</td>
<td>{$form->render($form['productItems'][$id]['description'])}</td>
<td>{$form->render($form['productItems'][$id]['remove'])}</td>
</tr>
{/foreach}
</tbody>
</table>
Editoval duskohu (27. 3. 2013 10:37)
- duskohu
- Člen | 778
Ahoj tak som prisiel na to co to sposobuje:
ked vytvaram formular, tak rovno naplnam ddata, takze v komponente
mam toto:
if ($this->productEntity) {
$form->setDefaults(array(
'productcategory_id' => $this->productEntity->productcategory_id,
'name' => $this->productEntity->name,
'description' => $this->productEntity->description,
'unitPrice' => $this->productEntity->unitPrice,
'discount' => $this->productEntity->discount,
'status' => $this->productEntity->status,
'newsTo' => $this->productEntity->newsTo,
));
foreach ($this->productEntity->related('productitem') as $productItem) {
$form['productItems'][$productItem->id]->setDefaults(
array(
'code' => $productItem->code,
'store' => $productItem->store,
'description' => $productItem->description,
'id' => $productItem->id,
));
}
}
Predpokladam ze polozka sa pri zmazani z containeru odstrani ale vo foreach naplnam data a nejako ju vytvori ale nenaplni datami, takze v containeri ostane ako prazdna polozka, ale preco mi to robi neviem :-(
Editoval duskohu (22. 5. 2013 17:14)
- Filip Procházka
- Moderator | 4668
Celé to opodmínkuj do if (!$form->isSubmitted()) {
aby se
to plnilo, jen když formulář není odeslaný.
- duskohu
- Člen | 778
Dik, ale to uz som skusal, ale stale nepomaha :-(
protected function attached($presenter)
{
if ($presenter instanceof Presenter) {
/** @var Form $form */
$form = $this['form'];
if ($this->productEntity) {
if (!$form->isSubmitted()) {
foreach ($this->productEntity->related('productitem') as $productItem) {
$form['productItems'][$productItem->id]->setDefaults(
array(
'code' => $productItem->code,
'store' => $productItem->store,
'description' => $productItem->description,
'id' => $productItem->id,
)
);
}
}
}
}
parent::attached($presenter);
}