Form onSuccess ignoruje funkci
- matronator
- Člen | 38
Už u toho sedim přes dvě hodiny a sem zoufalej… Ať dělám co dělám, form prostě při kliku na submit jenom refreshne stránku a vůbec nezaregistruje že je v presenteru nějaká funkce.
CityPresenter.php
<?php
declare(strict_types=1);
namespace App\FrontModule\Presenters;
use App\Model;
use App\Model\UserRepository;
use App\Model\DrugsRepository;
use Nette\Forms\Form;
use Tracy\Debugger;
use Nette\Application\BadRequestException;
use Nette\Utils\ArrayHash;
/////////////////////// FRONT: City PRESENTER ///////////////////////
final class CityPresenter extends BasePresenter
{
private UserRepository $userRepository;
private DrugsRepository $drugsRepository;
public function __construct(
UserRepository $userRepository,
DrugsRepository $drugsRepository
)
{
$this->userRepository = $userRepository;
$this->drugsRepository = $drugsRepository;
}
protected function startup()
{
parent::startup();
if (!$this->user->isLoggedIn())
$this->redirect('Login:default', ['backlink' => $this->storeRequest()]);
}
public function renderDarknet(): void
{
$drugs = $this->drugsRepository->findAll();
$this->template->drugs = $drugs;
$drugsInventory = [];
foreach($drugs as $drug) {
// $session = $this->session;
// $section = $session->getSection('price' . $drug->name);
// $section['price' . $drug->name] = $drug->price;
$amount = 0;
$exists = $this->drugsRepository->findUserDrug($this->user->getIdentity()->id, $drug->id)->fetch();
if ($exists) {
$amount = $exists->amount;
}
array_push($drugsInventory, $amount);
}
$this->template->drugsInventory = $drugsInventory;
}
public function createComponentDarknetForm(): Form
{
$drugs = $this->drugsRepository->findAll();
$form = new Form();
$form->setHtmlAttribute('class', 'uk-form-horizontal');
foreach($drugs as $drug) {
$form->addInteger($drug->name, $drug->name)
->setHtmlAttribute('class', 'uk-input uk-form-small')
->setHtmlAttribute('min', 0)
->addRule(Form::INTEGER, 'Input value must be a number');
}
$form->addSubmit('buy', 'Buy');
$form->addButton('sell', 'Sell');
$form->onSuccess[] = [$this, 'darknetFormSucceeded'];
return $form;
}
public function darknetFormSucceeded(Form $form, ArrayHash $values): void
{
Debugger::log('kokt');
}
}
City/darknet.latte
{define bodyClass}page--home{/define}
{define pageTitle}Darknet{/define}
{define metaDescription}Darknet{/define}
{block pageContent}
<div class="uk-container" uk-height-viewport="expand: true">
<div class="uk-width-expand" uk-margin>
<div class="uk-card uk-card-default uk-padding-remove-horizontal uk-padding-remove-top uk-card-body uk-overflow-auto">
{form darknetForm}
<table class="uk-table uk-table-small uk-table-hover uk-table-striped">
<thead>
<tr>
<th class="uk-text-nowrap uk-table-expand">Drug</th>
<th class="uk-text-nowrap">Price per unit</th>
<th class="uk-text-nowrap">Quantity</th>
<th class="uk-text-nowrap uk-table-shrink border-left">You have</th>
<th class="uk-text-nowrap uk-table-shrink border-left">Price before</th>
</tr>
</thead>
<tbody>
{for $i = 1; $i <= count($drugs); $i++}
<tr>
<td class="uk-text-nowrap">{label $drugs[$i]->name /}</td>
<td class="uk-text-nowrap"><strong>${$drugs[$i]->price} / g</strong></td>
<td class="uk-text-nowrap">{input $drugs[$i]->name}</td>
<td class="uk-text-nowrap border-left">{$drugsInventory[$i-1]}</td>
<td class="uk-text-nowrap border-left uk-text-right uk-text-light">${$drugs[$i]->past_price}</td>
</tr>
{/for}
</tbody>
</table>
<div class="uk-flex uk-flex-right uk-padding-small">
<button type="submit" class="uk-button uk-button-primary uk-margin-right" name="buy">Buy</button>
<button type="submit" class="uk-button uk-button-danger" n:name="sell">Sell</button>
</div>
{/form}
</div>
</div>
</div>
{/block}
- matronator
- Člen | 38
OMG, já sem věděl že to bude nějaká takováhle kktina, ehm, maličkost.. :D Díky moc, funguje!