发生验证错误时操作参数丢失 [英] Parameters to action lost when validation error occurs

查看:145
本文介绍了发生验证错误时操作参数丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个动作,newActioncreateAction.两者都具有相同的参数,这些参数是纯值,而不是对象.

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

它们的实现与此类似:

/**
 * 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
}

如果在不使用参数的情况下执行newAction,则一切正常,显示该表单时无需预填充字段.

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

如果提交的表单具有有效值,则会使用正确的值调用createAction,并且一切都会按预期进行.

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

如果表单提交的值无效,则验证将失败,这与预期的一样.然后,该请求将再次按原样转发给newAction-但是newAction的参数均为NULL,而不是之前输入到表单中的无效值.

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.

我已确保参数已实际提交(var_dump($_POST)),并且TYPO3已收到它们(通过在errorAction中打印它们).

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

为什么会这样,我做错什么了吗?还是extbase在那个地方坏了?

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

TYPO3版本:6.2.15

TYPO3-Version: 6.2.15

推荐答案

好吧,我刚刚找到了原因:故意这样做是为了避免传递验证失败的值. 这是伪造门票.

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.

为了获取无效的参数,可以使用$this->request->getOriginalRequest()访问原始请求.在上面的示例中,这样做看起来像这样:

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
}

这篇关于发生验证错误时操作参数丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆