handler to add input into form component
- richard
- Member | 60
Hi,
First of all thanks for previous prompt answers, they really help me to move
forward.
I need to add/remove inputs dynamically to/from a form component by clicking on
buttons. The true is I do not even know what would be the best way –
sessions, ajax, persistent components? It would be essential if the form
keeps values already filled in and just adds new input element. This
will be a form user can configure himself – i.e. buttons like add text
area, add text input and remove this input…
I was trying to do it via persistant component but I am not too sure if this
is a way. It does not work anyways…
Presenter:
<?php
use Nette\Application\UI\Form;
/**
* @persistent(form)
*/
class ConfiguratorPresenter extends BasePresenter
{
public function startup()
{
parent::startup();
}
protected function createComponentForm($name)
{
$form = new Form(NULL, 'form');
$form->addText('initialInput','Initial Input');
$form->addSubmit('save', 'Save');
$form->onSuccess[] = callback($this, 'save');
return $form;
}
public function save(Form $form)
{
// do something
}
public function handleAddInput($name,$label) {
$this->form->addText($name,$label);
}
public function handleRemoveInput($name) {
unset($this->form[$name])
}
}
?>
… and the manual form rendered would append
<a href="{removeInput!, $name}">remove input</a>
to
every input.
Thanks for any advice.
Last edited by richard (2011-08-24 05:38)