Dynamic text input in form
- vnovotny
- Member | 12
Hi guys, I need some help with adding text fields into existing Form. I need to do some tool for generating pdf report from tests given to some employee.
Imagine this form structure
FORM
--EmployeeName
--Year/month
--Test1
----Question1 -> Result1
----Question2 -> Result2
--Test2
----Question1 -> Result1
----Question2 -> Result2
----Question3 -> Result3
----Question4 -> Result4
--Test3
----Question1 -> Result1
----Question2 -> Result2
...
What i need is to add dynamicaly Tests and in every test also add dynamic
Questions.
I know that there is addDynamic extension by
Filip Procházka, but i don't get it.
All I need is some hint, maybe just sequence of steps, some image of whole process cycle, I don't know…
- HosipLan
- Moderator | 4668
If you don't want to stress yourself with complex rendering, you can use something like
$form->addDynamic('tests', function (Nette\Forms\Container $test) {
$test->addDynamic('questions', function (Nette\Forms\Container $question) {
$question->addTest('question', 'Question');
$question->addTest('answer', 'Answer');
});
});
Or, do it the “nice way”
$form->addDynamic('tests', function (Nette\Forms\Container $test) {
$test->addDynamic('questions', function (Nette\Forms\Container $question) {
$question->addTest('title', 'Question');
});
$test->addDynamic('answers', function (Nette\Forms\Container $question) {
$question->addTest('content', 'Answer');
});
});
Pass some helpfull information using template variables, or Control options, it's up to you
$this->template->testTitles = $testTitles;
{form tests}
{foreach $form['tests']->components as $testId => $test}
<h3>{$testTitles[$testId]}</h3>
{foreach $test->components as $questionId => $question}
{control 'tests-'.$testId.'-questions-'.$questionId.'-title'}
{control 'tests-'.$testId.'-answers-'.$questionId.'-content'}
{/foreach}
{/foreach}
{/form}
Or you want another version?
$form->addDynamic('tests', function (Nette\Forms\Container $test) {
$test->addDynamic('answers', function (Nette\Forms\Container $question) {
$question->addTest('answer', 'Question');
});
});
public function renderDefault()
{
$this->template->testTitles = $testTitles;
$this->template->questions = $questions; // array(testId => array(id => question))
}
{form tests}
{foreach $form['tests']->components as $testId => $test}
<h3>{$testTitles[$testId]}</h3>
{foreach $questions[$testId] as $questionId => $question}
<h4>{$question}</h4>
{control 'tests-'.$testId.'-answers-'.$questionId.'-answer'}
{/foreach}
{/foreach}
{/form}
Link to download the extension: https://componette.org/search/?…
Last edited by HosipLan (2012-01-20 10:28)