Z formuláře se nazavolá akce v presenteru
Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.
- svobodai
- Člen | 136
Mám presenter, v něm vytvářím komponentu, tu zobrazím v šabloně, ale když vyplním formulář a odešlu ho tak se nic nestane. Zatím chci vědět co mi formulář vrátí, ale nestane se nic, resp. překreslí se formulář, ale nevypíše se message ani se nezobrazí ten dump.
<?php
protected function createComponentCategoryForm() {
$form = new Form();
$form->addTextArea('category_text','Category text:',180,3)
->addRule(Form::FILLED, 'Category must be filled' );
$form->addTextArea('category_description', 'Description',180,10);
$form->addHidden('lang_id');
$form->addHidden('category_id');
if ($this->id > 0) {
$tmp_labels = $this->categoryLabel->findBy(array('lang_id' => $this->lang, 'category_id' => $this->id))->select('*')->fetch();
$formData = array('category_text' => $tmp_labels['category_text'],
'category_description' => $tmp_labels['category_description'],
'lang_id' => $tmp_labels['lang_id'],
'category_id' => $tmp_labels['category_id']);
$form['category_text']->setDefaultValue($tmp_labels['category_text']);
$form['category_description']->setDefaultValue($tmp_labels['category_description']);
$form['lang_id']->setDefaultValue($tmp_labels['lang_id']);
$form['category_id']->setDefaultValue($tmp_labels['category_id']);
$labelSubmit = "Update category";
}
else {
$form->addHidden('test_id');
$form['test_id']->setDefaultValue($this->test_ident);
$form->addText('category_order','Order')
->addRule(Form::FILLED,'Category order must be filled')
->addRule(Form::INTEGER,'Category order must be number');
$labelSubmit = "Insert category";
}
$form->addSubmit('submit', $labelSubmit);
$form['submit']->onClick[] = callback($this,'categoryFormSubmitted');
return $form;
}
public function categoryFormSubmitted(Form $form) {
$this->presenter->flashMessage('Test vlozeni');
$values = $form->getValues();
Debugger::dump($values);
die;
?>
- castamir
- Člen | 629
2 věci:
- co přesně tam vytváříš za třídu v
$form = new Form();
? Nette\Forms\Form nebo Nette\Application\UI\Form? Správně má být to druhé. - V případě použití
onClick[]
příjímá callback jako parametr nikoliv formulář, ale právě ten submitt button, který to odeslal. Správně viz ukázka
// uvnitř komponenty:
$form->onSuccess[] = callback($this,'categoryFormSubmitted');
// nebo
$form->onSuccess[] = $this->categoryFormSubmitted;
public function categoryFormSubmitted(Form $form) {
//...
}
V případě, že trváš na onClick[], pak použij následující:
public function categoryFormSubmitted(\Nette\Forms\Controls\SubmitButton $button) {
$form = $button->getForm();
//...
}
Editoval castamir (3. 4. 2013 23:33)
- svobodai
- Člen | 136
Jo to pomohlo, když jsem dal že je to AppForm tak už je to lepší.
Problém je, že se nedostanu do té funkce, kde by mělo dojít ke zpracování
dat z formuláře.
<?php
protected function createComponentCategoryForm() {
$form = new AppForm();
$form->addTextArea('category_text','Category text:',180,3)
->addRule(Form::FILLED, 'Category must be filled' );
$form->addTextArea('category_description', 'Description',180,10);
$form->addHidden('lang_id');
$form->addHidden('category_id');
(int)$tmpId = $this->id;
if ($tmpId == 0) {
Debugger::dump($tmpId);
$form->addHidden('test_id');
$form['test_id']->setDefaultValue($this->test_ident);
$form->addText('category_order','Order')
->addRule(Form::FILLED,'Category order must be filled')
->addRule(Form::INTEGER,'Category order must be number');
$labelSubmit = "Insert category";
}
else {
$tmp_labels = $this->categoryLabel->findBy(array('lang_id' => $this->lang, 'category_id' => $this->id))->select('*')->fetch();
$formData = array('category_text' => $tmp_labels['category_text'],
'category_description' => $tmp_labels['category_description'],
'lang_id' => $tmp_labels['lang_id'],
'category_id' => $tmp_labels['category_id']);
$form['category_text']->setDefaultValue($tmp_labels['category_text']);
$form['category_description']->setDefaultValue($tmp_labels['category_description']);
$form['category_id']->setDefaultValue($tmp_labels['category_id']);
$form['lang_id']->setDefaultValue($tmp_labels['lang_id']);
$labelSubmit = "Update category";
}
$form->addSubmit('submit', $labelSubmit);
$form->onSuccess[] = $this->categoryFormSubmitted;
return $form;
}
public function categoryFormSubmitted(AppForm $form) {
$this->presenter->flashMessage('Test vlo6en9');
$values = $form->getValues();
Debugger::dump($values);
die;
}
?>
Editoval svobodai (5. 4. 2013 20:09)