[go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug that FormRequest could't get the changed data from Context. #3351

Merged
merged 5 commits into from
Mar 10, 2021

Conversation

wdaglb
Copy link
Contributor
@wdaglb wdaglb commented Mar 10, 2021

表单数据

{
	"realname": "  名字  ",
	"province": "省份  "
}

例如我在中间件处理一下空格字符串的数据

class TrimStringsMiddleware implements MiddlewareInterface
{

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $data = $request->getParsedBody();
        $data = array_map('trim', $data);

        $request = $request->withParsedBody($data);

        return $handler->handle($request);
    }
}

控制器会无法获取格式化之后的数据

    /**
     * 创建或修改收货地址
     *
     * @param UserAddressRequest $request
     * @return array|bool[]
     *
     * @PostMapping("/user/address")
     */
    public function post(UserAddressRequest $request)
    {
        $form = $request->validated();
        return $form;
    }

本次提交即修复这个问题。

@limingxinleo limingxinleo changed the title bugfix: 修复FormRequest获取不到withParsedBody修改后数据 Fixed bug that FormRequest does not get the changed data from Context. Mar 10, 2021
@limingxinleo
Copy link
Member

试试这个代码

@limingxinleo
Copy link
Member

你之前的代码是有问题的,FormRequest 是个代理,如果直接 setRequest 就会导致数据污染。

@wdaglb
Copy link
Contributor Author
wdaglb commented Mar 10, 2021

你之前的代码是有问题的,FormRequest 是个代理,如果直接 setRequest 就会导致数据污染。

OK,学习了Context还能这样用

@limingxinleo limingxinleo changed the title Fixed bug that FormRequest does not get the changed data from Context. Fixed bug that FormRequest could't get the changed data from Context. Mar 10, 2021
@limingxinleo limingxinleo merged commit 2ef0670 into hyperf:master Mar 10, 2021
@limingxinleo
Copy link
Member

合进来了,你拉最新代码试试

@wdaglb
Copy link
Contributor Author
wdaglb commented Mar 10, 2021

ok,解决了

@wdaglb wdaglb deleted the bugfix branch March 10, 2021 03:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants