TYPO3 Extbase Controller with new/createAction not showing form data after invalid validation

Viewed 11

I am trying to create an Extbase extension in TYPO3 10.4 which fetches and inserts records from an external API. I've got most of it working, but am stuck on one thing. I'm converting the data I'm getting from the API to DTO models and also use those when creating a new record for the API. Just like I'd do with models created from/for database records I've got a newAction with a form in Fluid and createAction which sends the data to a service, which sends it to the API. The DTO model has validation, which is handled correctly. If the form data isn't valid, the user is forwarded to the newAction correctly. However the data isn't filled into the form anymore. I've got a feeling I'm forgetting something small, but as far as I can see it should work.

My model looks like (simplified):

<?php
declare(strict_types=1);

namespace Vendor\MyExtension\Controller;

use TYPO3\CMS\Extbase\Annotation as Extbase;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use Vendor\MyExtension\Domain\Model\Dto\Item;
use Vendor\MyExtension\Service\ItemService;

class MyController extends ActionController
{
    /**
     * @var ItemService
     */
    protected ItemService $itemService;

    /**
     * @return void
     */
    public function listAction(): void
    {
        $this->assign('items', $this->itemsService->getItems());
    }

    /**
     * @param Item $item
     * @return void
     * @Extbase\IgnoreValidation("service")
     */
    public function newAction(Item $item = null): void
    {
        $this->assign('item', $item);
    }

    /**
     * @param Item $item
     * @return void
     */
    public function createAction(Item $item): void
    {
        $this->itemService->add($item);

        $this->redirect('list');
    }
}

The DTO model looks like:

<?php
declare(strict_types=1);

namespace Vendor\MyExtension\Domain\Model\Dto;

class Item
{
    /**
     * @var string
     */
    protected string $value1;

    /**
     * @var string
     * @TYPO3\CMS\Extbase\Annotation\Validate("NotEmpty")
     */
    protected string $value2;

    /**
     * @return string
     */
    public function getValue1(): string
    {
        return $this->value1;
    }

    /**
     * @param string $value1
     */
    public function setValue1(string $value1): void
    {
        $this->value1 = $value1;
    }

    /**
     * @return string
     */
    public function getValue2(): string
    {
        return $this->value2;
    }

    /**
     * @param string $value2
     */
    public function setValue2(string $value2): void
    {
        $this->value2 = $value2;
    }
}

And the Fluid template for the form:

<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers">
<f:layout name="Default" />

<f:section name="Content">
    <f:form action="create" method="post" object="{item}" name="item">
        <f:form.validationResults>
            <f:if condition="{validationResults.flattenedErrors}">
                <ul class="errors">
                    <f:for each="{validationResults.flattenedErrors}" as="errors" key="propertyPath">
                        <li>{propertyPath}
                            <ul>
                                <f:for each="{errors}" as="error">
                                    <li>{error.code}: {error}</li>
                                </f:for>
                            </ul>
                        </li>
                    </f:for>
                </ul>
            </f:if>
        </f:form.validationResults>
        <div class="form-group">
            <label for="value1">Value 1</label>
            <f:form.textfield property="value1" id="value1" class="form-control"/>
        </div>
        <div class="form-group">
            <label for="value2">Value 2</label>
            <f:form.textfield property="value2" id="value2" class="form-control"/>
        </div>
        <button type="submit" class="btn btn-primary">Save</button>
    </f:form>
</f:section>
</html>

If I debug $item in newAction it is always null, even after submitting the form with invalid data (empty value2 field). It does show the expected validation result.

0 Answers
Related