Form submit using component's method and AJAX
- fronty
- Member | 16
Hi,
I have problem with form submit using component's method added as form's onSuccess callback in AJAX.
Presenter:
<?php
// Form dependencies setter
public function getForm(FT\Forms\Form $form = NULL) {
if ($form === NULL) $form = new FT\Forms\Form();
$form->setTranslator($this->translator);
return $form;
}
// Component factory
protected function createComponentSearchDMSClient($name) {
$form = $this->getForm();
$search = new App\SearchDMSClient($this, $name, $form);
$search->setTranslator($this->translator);
return $search;
}
?>
SearchDMSClient.php component:
<?php
protected $form;
public function __construct(Nette\ComponentModel\IContainer $parent, $name, FT\Forms\Form $form) {
parent::__construct($parent, $name);
$this->setForm($form);
}
public function setForm(FT\Forms\Form $form) {
$form->getControlElement()->addClass('ajax');
$form->addText('ic', 'client.ic')->getControlPrototype()->setPlaceholder('client.ic');
$form->addText('name', 'client.name')->getControlPrototype()->setPlaceholder('client.name');
$form->addText('surname', 'client.surname')->getControlPrototype()->setPlaceholder('client.surname');
$form->addSubmit('_send', 'forms.search');
$form->onSuccess[] = $this->proccessForm;
return $this;
}
public function proccessForm(FT\Forms\Form $form) {
$values = $form->getValues(TRUE);
echo '<pre>';
print_r($values);
exit();
}
?>
FT\Forms\Form
is my implementation of
Nette\Forms\Form
, it only adds some custom control factories.
When I remove AJAX, method proccessForm is called correctly, but in AJAX
mode nothing happens. AJAX request is done, but component's method isn't called
at all.
I suppose, that this problem has something to do with linking to parent
presenter, so I tried also late tighting (without $parent and $name parameters
in SearchDMSClient's __construct), but it also worked only without AJAX.
Could you please point me to right direction?
Thank's to all in advance.
- fronty
- Member | 16
OK, problem solved. It wasn't a problem of linking to presenter, but form custom template. I registered _send as input[type=“submit”], but rendered as this:
<button name="_send" class="btn btn-default">
<span class="fa fa-search"></span> {_forms.search}
</button>
That's quite a big problem of Nette forms, it doesn't have any implementation of <button> as form submit. I know there can be mismatch on server-side interpretation of sent value, but even so.
- Pavel Kravčík
- Member | 1195
echo '<pre>';
print_r($values);
exit();
//faster
dump($values); exit;
Did you mean n:name=“_send” or {input _send}
?