[TYPO3-dev] Extbase: Create multiple objects within one formular

Björn b.rieger90 at gmx.de
Thu May 16 16:46:11 CEST 2013


Hello,
I am developing an extension. But right now I am facing a problem, which I can't solve by myself. I want to create multiple objects of one class within one formular.
With the extension it should be possible to set up and answer surveys. For each single answer to each single questions of the survey an instance of the just called object shall be created.
So for example there are multiple TextFields and I want to create one object and fill its variable "value" for each field.

I already found a link according that problem:
Sorry that it's in german, but maybe you can get the idea.
http://www.typo3.net/forum/beitraege/extbase_und_fluid/106852/
I guess this should be the solution. But I am not able to adopt that to my code.

The only thing that happens, when I click the Submit Button at the end of the formular is, that the view is reloaded.
In the following I post my code:
For explanation, the TextFields are created in a external Partial, which is called by a Partial-VH.

Show:
{namespace vh=Tx_survey_ViewHelpers}

<f:layout name="Default" />

<f:section name="main">

    <f:form method="post" controller="Session" action="create"
        name="newSession" object="{newSession}" arguments="{survey: survey}">

(...)

            <fieldset>
                <legend>{question.text}</legend>
                <div>{question.subtext}</div>
                <br />

(...)

                        <f:render partial="{vh:partial(type: '{question.type}')}"
                            arguments="{question: question,
                                    options: question.options}" />
(...)

            </fieldset>
            <br />
        </f:for>

        <f:form.submit value="Submit" />

    </f:form>

</f:section>


TextField Partial:

<f:form.textfield name="tx_survey_survey[answers][value][]" value="" />
<f:form.hidden name="tx_survey_survey[answers][qid][]" value="2" />


SessionController:

<?php
namespace Stupro\Survey\Controller;
/**
 *
 *
 * @package survey
*
*/
class SessionController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
    
    /**
     * sessionRepository
     *
     * @var \Stupro\Survey\Domain\Repository\SessionRepository
     * @inject
     */
    protected $sessionRepository;
    
(...)

    /**
     * Creates a new session
     *
     * @param Stupro\Survey\Domain\Model\Survey $survey The survey the session belongs to
     * @param Stupro\Survey\Domain\Model\Session $newSession A fresh session object which has not yet been added to the repository
     * @param array $answers An array of answers. array(amount => '12.50', comment => 'Children')
     * @return void
     * @dontverifyrequesthash
     */
    public function createAction(Stupro\Survey\Domain\Model\Survey $survey, \Stupro\Survey\Domain\Model\Session $newSession, array $answers = array()) {

//         $newSession = $this->createAndAddAnswers($newSession, $answers);
        $newSession->setSurvey($survey);
        
        $survey->addSession($newSession);
        $this->sessionRepository->add($newSession);
        
        $this->redirect('list', 'Session');
    }

    /**
     * Creates an answer for every item in the answers array and adds it to the session.
     *
     * @param Stupro\Survey\Domain\Model\Session $session The sessions the answers should be added to
     * @param array $answers An array of answers. array(amount => '12.50', comment => 'Children')
     * @return Stupro\Survey\Domain\Model\Session The Session
     */
    protected function createAndAddAnswers(Stupro\Survey\Domain\Model\Session $session, array $answers = array()) {
        foreach ($answersas $key => $value) {
            if (!empty($value)) {
                $session->addAnswer(new Stupro\Survey\Domain\Model\Answer($value, $answers['qid'][$key]));
            }
        }
        return $session;
    }

}
?>


I am very thankful for each kind of help!
If anyone may be able to help me or has an idea. I can upload the several files, for a better overview.

Greetz!



More information about the TYPO3-dev mailing list