简体   繁体   中英

Validate parameters, set by $this->request->setArgument()

I have a controller action, which accepts 3 parameters: 1st is built from Fluid form, 2 others are created in initialize*() action and provided to request.

protected function initializeCombinedAction()
{
    // creation of ObjectStorage's $firstList and $secondList
    $this->request->setArgument('firstList', $firstList);
    $this->request->setArgument('secondList', $secondList);
}

/**
 *
 * @param \Vendor\Ext\Domain\Model\MyModel $mymodel
 * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Vendor\Ext\Domain\Model\SubModel>    $firstList
 * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Vendor\Ext\Domain\Model\SubModel>    $secondList
 */
public function combinedAction(Model\MyModel $mymodel, $firstList, $secondList)
{
// some code here
}

$firstList and $secondList created as arrays as if they were provided in request. PropertyMapper recognizes them and maps correctly, resulting in two ObjectStorage 's holding instances of SubModel are available as action arguments.

But the problem is, that no validation of SubModel is made. With 1st argument validators are executed, but not for 2nd and 3rd.

Shoould I somehow explicitly define such validation?

The problem should be that there are no default validators for the ObjectStorage. You can always set the validation yourself like that:

 * @validate $firstList \Vendor\Ext\Domain\Validator\ListValidator

Thanks to Dimitri L. , I was pointed into right direction.

The answer is: Extbase validates all the action parameters, no matter how they are constructed, but it doesn't automatically resolve validators for parameters of ObjectStorage type.

Luckily there is already built-in CollectionValidator , which can be used exactly for this purpose.

So, my initial action signature will look like this:

/**
 * @param \Vendor\Ext\Domain\Model\MyModel $mymodel
 * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Vendor\Ext\Domain\Model\SubModel>    $firstList
 * @param \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\Vendor\Ext\Domain\Model\SubModel>    $secondList
 * @validate $firstList Collection(elementType=\Vendor\Ext\Domain\Model\SubModel)
 * @validate $secondList Collection(elementType=\Vendor\Ext\Domain\Model\SubModel)
 */
public function combinedAction(Model\MyModel $mymodel, $firstList, $secondList)
{
// some code here
}

Additionally I've created an issue on TYPO3 Forge: #78546 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM