[TYPO3-mvc] create two objects with one form and validation

Steffen Müller typo3 at t3node.com
Fri Mar 23 15:26:35 CET 2012


Hi.

I once solved a similar issue like this:

Model: Person <-- 1:1 --> Address


Controller:
---------------

/**
 * Preprocessing Arguments from Request for handling Aggregate
 *
 * @return void
 */
public function initializeAction() {
  if ($this->request->hasArgument('personAggregate')) {
    $personAggregateArguments =
$this->request->getArgument('personAggregate');
    foreach ($personAggregateArguments as $key => $arguments) {
      $this->request->setArgument($key, $arguments);
    }
  }
}

/**
 * action create
 *
 * @param $newPerson
 * @param $newAddress
 * @return void
 */
public function createAction(Tx_DemoMulti_Domain_Model_Person
$newPerson, Tx_DemoMulti_Domain_Model_Address $newAddress) {
  $newPerson->addAddress($newAddress);
  $this->personRepository->add($newPerson);
  $this->flashMessageContainer->add('Your new Person was created.');
  $this->redirect('list');
}

Template:
-----------

<f:form action="create" name="personAggregate" object="{personAggregate}">

  <label for="name"><f:translate
key="tx_demomulti_domain_model_person.name" />:</label>
  <f:form.textfield property="newPerson.name" />

  <label for="name"><f:translate
key="tx_demomulti_domain_model_address.street" />:</label>
  <f:form.textfield property="newAddress.street" />

  <label for="name"><f:translate
key="tx_demomulti_domain_model_address.city" />:</label>
  <f:form.textfield property="newAddress.city" />

<f:form.submit value="Create new" />
</f:form>



The validation itself works, except that the propertyMapper is not able
to prefill form fields and errors.




-- 
cheers,
Steffen

TYPO3 Blog: http://www.t3node.com/
Twitter: @t3node - http://twitter.com/t3node


More information about the TYPO3-project-typo3v4mvc mailing list