Problem s addProtection ve formulari pri odesilani dat pomoci JS pri prechodu z nette 2.0.14 na 2.1.0
- vosy
- Člen | 532
ahoj po prechodu z nette 2.0.14 na 2.1.0 mam problem s odesilanim dat z formular pomoci js a to presne s zobrazeni hlasky:
$form->addProtection('Vypršel časový limit, odešlete formulář znovu');
HomepagePresenter:
<?php
namespace App\Presenters;
use Nette,
App\Model;
/**
* Homepage presenter.
*/
class HomepagePresenter extends BasePresenter
{
public $FormFactory;
function injectThis(
\App\Factories\FormFactory $FormFactory
)
{
$this->FormFactory = $FormFactory;
}
public function startup()
{
parent::startup();
$this->session->start();
}
public function renderDefault()
{
}
/* FACTORIES */
protected function createComponentDefForm()
{
$fData = array('parent'=>$this);
$form = $this->FormFactory->defForm($fData);
return $form;
}
/* HANDLES */
function handleDefValidate()
{
$post = $this->request->post;
$form = $this['defForm'];
$form->setDefaults($post['form']);
$form->validate();
$return = array();
if($form->hasErrors())
{
foreach($form->getErrors() as $v1)
{
$return['chyba'][] = $v1;
}
}
print_r($return);
$this->terminate();
}
}
FormFactory:
<?php
namespace App\Factories;
use \Nette\Application\UI\Form;
class FormFactory
{
public function defForm($opt=array())
{
$parent = $opt['parent'];
$form = new Form;
$form->addText('kod', 'Kod:')
->setRequired('Zadejte kod zájezdu.');
$form->addSubmit('ulozit', 'Uložit');
$form->addSubmit('storno', 'Storno')
->setValidationScope(FALSE)
->onClick[] = callback($this, 'pridatFormStorno');
$form->addProtection('Vypršel časový limit, odešlete formulář znovu');
// $form->onValidate[] = array($this, 'pridatFormValidate');
// $form->onSuccess[] = callback($parent, 'pridatFormUlozit');
return $form;
}
public function pridatFormStorno(\Nette\Forms\Controls\SubmitButton $button)
{
$form = $button->getForm();
$presenter = $form->getPresenter();
$presenter->redirect('this');
}
}
main.js
$(function(){
$(document).on('click', 'input[name="ulozit"]', function()
{
var pData = {form:{kod:$('#frm-defForm-kod').val()}};
var url = $(this).data('url');
console.log(pData);
console.log(url);
$.post(url, pData, function(r)
{
console.log(r);
});
return false;
});
});
v nette 2.0.*
se mi pri nevyplneni formulare zobrazi pouze chyba: „Zadejte kod
zájezdu.“
ale v nette 2.1.*
se zobrazi i „Vypršel časový limit, odešlete formulář znovu.“
tato hlaska se zobrazuje i pri spravne vyplnenym form :(
prijde mi jako ze si script mysli ze na nej nekdo utoci, ci co.
dix
- David Matějka
- Moderator | 6445
mas spravne to id ‚#frm-defForm-kod‘? v 2.1 se zpusob generovani id menil
- David Matějka
- Moderator | 6445
zkus si dumpnout session, jestli v nem jsou spravna data. zkus to bez ajaxu, zkus aktualizovat na 2.1.2
- David Matějka
- Moderator | 6445
jo, uz to vidim.. ty predavas jen „kod“ a ne ten csrf token. Proc nepouzijes nette.ajax.js?
- David Matějka
- Moderator | 6445
csrf token se predava pres post (resp. get) a pak se kontroluje proti session, kde je (mel by byt) ten samy token a k nemu nejake informace (cas vytvoreni, formular, na ktery se vaze atd.)
- David Matějka
- Moderator | 6445
neres to pres nejaky handle, navaz si normalne na formular onSuccess event (pripadne onClick na button)
- David Matějka
- Moderator | 6445
vosy napsal(a):
nedari se mi to :/
a zkousel si to vypnout a znovu zapnout?
- vosy
- Člen | 532
mam ve formulari
$form->addSubmit('ulozit', 'Uložit');
$form->onSuccess[] = callback($this, 'pridatFormUlozit');
v js
$(function(){
$.nette.init();
});
v sablone
<div class="form-horizontal">
{form defForm class=>"ajax"}
<div>
a vysledek
<form action="/zkNette21/formulare2/www/?do=defForm-submit" method="post" id="frm-defForm" class="ajax">
takze kdyz kliknu na ulozit vola se mi
http://localhost/…mulare2/www/?…
proc se tedy nevola
$form->onSuccess[] = callback($this, ‚pridatFormUlozit‘);
- David Matějka
- Moderator | 6445
no to se zavola pri zpracovani toho signalu. zkus si v tom callbacku poslat neco do payloadu a uvidis, jestli se ti to spravne vrati
- vosy
- Člen | 532
Remote Address:::1:80
Request URL:http://localhost/zkNette21/formulare2/www/?do=defForm-submit
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:cs-CZ,cs;q=0.8
Connection:keep-alive
Content-Length:77
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:nette-debug-panel-Nette-Database-Diagnostics-ConnectionPanel=; PHPSESSID=2o3pp6hu0s7jo47j6c3ie53da1; nette-browser=mf2qbywuuy
Host:localhost
Origin:http://localhost
Referer:http://localhost/zkNette21/formulare2/www/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36
X-Requested-With:XMLHttpRequest
Query String Parametersview sourceview URL encoded
do:defForm-submit
Form Dataview sourceview URL encoded
kod:dddd
_token_:xs4pg3jhxfODN9ZZO6kDlNhuGBGJ3sRJ7gj0E=
ulozit:Uložit
:
Response Headersview source
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:Keep-Alive
Content-Encoding:gzip
Content-Length:727
Content-Type:text/html; charset=utf-8
Date:Thu, 24 Apr 2014 11:39:24 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive:timeout=5, max=98
Pragma:no-cache
Server:Apache/2.2.26 (Unix) DAV/2 PHP/5.4.24 mod_ssl/2.2.26 OpenSSL/0.9.8y
Set-Cookie:PHPSESSID=2o3pp6hu0s7jo47j6c3ie53da1; expires=Thu, 08-May-2014 11:39:24 GMT; path=/; httponly
Set-Cookie:nette-browser=mf2qbywuuy; path=/; httponly
Vary:X-Requested-With,Accept-Encoding
X-Frame-Options:SAMEORIGIN
X-Powered-By:Nette Framework
je tam furt do:defForm-submit
takovou fci nemam a s pomlckou fci asi ani neudelam
Editoval vosy (24. 4. 2014 13:58)
- David Matějka
- Moderator | 6445
a body response?
to ?do=defForm-submit
je spravne, to je interni nette signal pro
formular