Checkboxy u položek DB ke smazání
- RobK
- Člen | 6
Zdravím,
snažím se podle dokumentace dát dohromady formulář na mazání položek v DB, ale bohužel mi metoda getHttpFata vrací prázdné pole (zkoušel jsem $marked dumpovat). Tím pádem to do cyklu foreach ani nevleze. Moc díky za radu.
Tohle mám v šabloně:
{block content}
<div id="banner">
<h1 n:block=title>NT1 application</h1>
</div>
<div id="content">
<table>
<tr>
<th>Datum</th>
<th>Autor</th>
<th>E-mail</th>
<th>Komentář</th>
</tr>
{foreach $comments as $comment}
<tr>
<td><input type=checkbox name="marked[]" value={$comment->id}></td>
<td>{$comment->created_at|date:'F j, Y'}</td>
<td>{$comment->name}</td>
<td>{$comment->email}</td>
<td>{$comment->content}</td>
</tr>
{/foreach}
</table>
{control deleteForm}
<p><a n:href="Homepage:default">zpet na vypis prispevku</a></p>
</div>
{/block}
Tohle pak v prezenteru:
<?php
protected function createComponentDeleteForm()
{
$form = new Form; // means Nette\Application\UI\Form
$form->addSubmit('submit', 'Smazat označené');
$form->onSuccess[] = [$this, 'deleteFormSucceeded'];
return $form;
}
public function deleteFormSucceeded($form, $values)
{
$marked = $form->getHttpData($form::DATA_TEXT, 'marked[]');
//$marked = $form->getHttpData($form:: DATA_TEXT | $form::DATA_KEYS, 'marked[]');
foreach ($marked as $to_delete){
$count_deleted = $this->database->table('comments')
->where('id',$to_delete)
->delete();
}
$this->flashMessage('Uspesne smazano', 'success');
$this->redirect('this');
}
?>