使用Javascript在Extbase/Fluid TYPO3中向ObjectStorage添加/创建元素

3

在使用JavaScript动态创建Fluid表单中的新子元素的正确方式是什么?

问题: 使用Extbase ObjectStorages的1:n关系(父/子): 当调用父Fluid表单时,应该能够添加多个子元素(包括属性,当然!)

肮脏的、部分工作的解决方案: 我添加了一些JS代码并动态添加了所需的输入元素。 "xxx"将为每个孩子进行迭代。数据将被正确地存储在数据库中。

<input type="text" placeholder="First Name" name="tx_booking[newBooking][accompanyingperson][xxx][firstname]">

然而,如果出现错误,所有子表单都会消失,并且不会显示任何f3-form-error。这可能是因为重定向到originalRequest(没有子字段的初始表单)的原因。
如何处理此问题,而不使用不道德的手段呢? 请给我一些简短的提示。

我有完全相同的问题,但到目前为止还没有找到解决方案。 - Christian Fries
2个回答

0

再次,我将自己回答这个问题!以下几行是最糟糕的代码,但它能够工作。我真的想知道如何以正确的方式做到这一点。然而,解决方案是从动态添加的JS输入中获取参数。这是在errorAction中完成的,并将通过forward()传递给初始操作,其中应该显示错误。

我认为,使用PropertyMapper并修改trustedProperties必须有更好的方法....

这里是一个简短的例子:

    // Error function in Controller

protected function errorAction() {

    $referringRequest = $this->request->getReferringRequest();

    // Manual added JS Data
    if($this->request->hasArgument('newRegistration'))
    {
        $newRegistration = $this->request->getArgument('newRegistration');
        $referringRequest->setArgument('accompanyingperson', $newRegistration['accompanyingperson']);
    }

     if ($referringRequest !== NULL) {
        $originalRequest = clone $this->request;
        $this->request->setOriginalRequest($originalRequest);
        $this->request->setOriginalRequestMappingResults($this->arguments->getValidationResults());
        $this->forward($referringRequest->getControllerActionName(), $referringRequest->getControllerName(), $referringRequest->getControllerExtensionName(), $referringRequest->getArguments());

    }
}


// action new in Controller
public function newAction(\***\***\Domain\Model\Registration $newRegistration = NULL) {
    if($this->request->hasArgument('accompanyingperson'))
    {
        $this->view->assign('accPer', $this->request->getArgument('accompanyingperson'));
    }
    .
    .
    .
}


//Fluid template of Action New
<f:if condition="{accPer}">
    <f:for each="{accPer}" as="ap" key="key" iteration="i">
        <f:form.textfield class="form-control" placeholder="First Name" property="accompanyingperson.{key}.firstname"/>
        .
        .
        .
    </f:for>
</f:if>

0
以下是我的解决方案,类似于你的。
模型
class Resume extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {
    /**
     * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<Builder>
     * @cascade remove
     */
    protected $builders;
}

class Builder extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity {
    /**
     * @var string
     */
    protected $title;
}

控制器

class ResumeController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
    /**
     * @var \Dagou\Resume\Domain\Repository\ResumeRepository
     * @inject
     */
    protected $resumeRepository;

    /**
     * @param \Dagou\Resume\Domain\Model\Resume $resume
     * @see \Dagou\Resume\Controller\ResumeController::saveAction()
     */
    protected function createAction(\Dagou\Resume\Domain\Model\Resume $resume = NULL) {
        $this->view->assignMultiple([
            'resume' => $resume,
        ]);
    }

    protected function initializeCreateAction() {
        if (($request = $this->request->getOriginalRequest())) {
            $this->request->setArgument('resume', $request->getArgument('resume'));

            $propertyMappingConfiguration = $this->arguments->getArgument('resume')->getPropertyMappingConfiguration();

            $propertyMappingConfiguration->allowCreationForSubProperty('builders.*');

            $propertyMappingConfiguration->allowProperties('builders')
                ->forProperty('builders')->allowAllProperties()
                    ->forProperty('*')->allowAllProperties();
        }
    }

    protected function initializeSaveAction() {
        $propertyMappingConfiguration = $this->arguments->getArgument('resume')->getPropertyMappingConfiguration();

        $propertyMappingConfiguration->allowCreationForSubProperty('builders.*');

        $propertyMappingConfiguration->allowProperties('builders')
            ->forProperty('builders')->allowAllProperties()
                ->forProperty('*')->allowAllProperties();
        }
    }

    /**
     * @param \Dagou\Resume\Domain\Model\Resume $resume
     */
    protected function saveAction(\Dagou\Resume\Domain\Model\Resume $resume) {
        $this->resumeRepository->add($resume);
    }
}

模板

<f:form class="form-horizontal" name="resume" action="save" object="{resume}">
    <f:if condition="{resume.builders}">
        <f:for each="{resume.builders}" as="builder" iteration="builderIteration">
            <f:form.textfield class="form-control" property="builders.{builderIteration.index}.header" />
        </f:for>
    </f:if>
</f:form>

如果你有更好的,请告诉我。谢谢!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接