Parameters to action lost when validation error occurs

自闭症网瘾萝莉.ら 提交于 2019-12-11 03:54:49

问题


I have two actions, newAction and createAction. Both get the same parameters, which are plain values, not objects.

Their implementations looks similar to this:

/**
 * Renders a form with the createAction as action="".
 *
 * @param string $value1
 * @param boolean $value2
 *
 * @ignorevalidation $value1
 * @ignorevalidation $value2
 */
public function newAction($value1 = NULL, $value2 = NULL) {
    $this->view->assignMultiple([
        'value1' => $value1,
        'value2' => $value2,
    ]);
}

/**
 * @param string $value1
 * @param boolean $value2
 *
 * @validate $value1 NotEmpty
 * @validate $value1 EmailAddress
 * @validate $value1 \Vendor\Extkey\Validator\MyValidator
 *
 * @validate $value2 Boolean(is=TRUE)
 */
public function createAction($value1, $value2) {
    // Do some stuff
}

If the newAction is executed without parameters, everything works fine, the form is displayed without prefilling the fields.

If the form is submitted with valid values, the createAction is called with the correct values, and everything works as expected.

If the form is submitted with invalid values, validation fails, as expected. Then the request is forwarded to the newAction again, as it should - but the parameters for the newAction are all NULL, instead of the invalid values that were entered into the form before.

I've made sure that the parameters are actually submitted (var_dump($_POST)), and TYPO3 has received them (by printing them in the errorAction).

Why does this happen, am I doing something wrong? Or is extbase broken in that place?

TYPO3-Version: 6.2.15


回答1:


Ok, I have just found the reason for this: It is intentionally done this way, to avoid passing around values that failed validation. Here is the forge ticket.

In order to get the invalid parameters, one can access original request using $this->request->getOriginalRequest(). Doing this in my above example could look like this:

/**
 * Renders a form with the createAction as action="".
 * No more parameters here!
 */
public function newAction() {
    $originalRequest = $this->request->getOriginalRequest();
    if (NULL !== $originalRequest) {
        $this->view->assignMultiple($originalRequest->getArguments());
    }
}

/**
 * @param string $value1
 * @param boolean $value2
 *
 * @validate $value1 NotEmpty
 * @validate $value1 EmailAddress
 * @validate $value1 \Vendor\Extkey\Validator\MyValidator
 *
 * @validate $value2 Boolean(is=TRUE)
 */
public function createAction($value1, $value2) {
    // Do some stuff
}


来源:https://stackoverflow.com/questions/32936733/parameters-to-action-lost-when-validation-error-occurs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!