Closed ledahu closed 3 years ago
add: if i add this middleware to the pure skeleton, after a failed login i didnt get the Post Var still empty !
Have you checked the Content-Type?
https://www.slimframework.com/docs/v4/middleware/body-parsing.html#media-type-detection
yes, as you can see, the bodyParsingMiddleware is in the stack. Try it, you will see that after LoginSubmitAction, the var_dump stay empty
I have already seen that you have added the BodyParsingMiddleware. That's why my question was if you checked the "Content-Type" inside the request for one of the supported media type. The BodyParsingMiddleware supports only POST requests.
So it will expect the body data to standard form format (application/x-www-form-urlencoded
) by default.
If you are sending JSON to this route, then you need to set the Content-type header to application/json
.
i do not send json. I just want that after a post submit , the middleware push the fiels in addGlobals to let it set as default value in twig field is submit raise validation errors
so, in slim3 , i had a
`<?php
namespace App\Middleware;
class OldinputMiddleware extends Middleware { /**
@return mixed */
public function __invoke($request, $response, $next) { if(isset($_SESSION['old'])) $this->container->view->getEnvironment()->addGlobal('old',$_SESSION['old']);
$_SESSION['old']=$request->getParams();
$response=$next($request,$response);
return $response;
} }`
now, to succesfull do it, i must set the session in the Action, but in theory i would like to get the parsedbody in a middleware may it come frome the use of responder->redirect ? the point i dont understant is that if i dump $request, i get the pasedbody array in it the dump (protected) but if i dump $request->getParsedBody it is empty
how to check the content-type by a dump ?
After a HTTP redirect the HTTP method is GET
(and not POST anymore), so the submitted form data will always be empty after a redirect. HTTP is designed like this. If you want to keep a state from the last request you have to store it for example in a session.
As long as your form is in POST mode, you can render the form data directly into template again, without sessions.
A "better" option would be to send only the form data via Ajax to the server for input validation and storage in the database.
Generally I would try to avoid saving form data into a session, but I don't have enough context to give an accurate answer.
It's just to let the user don't have to rewrite if form fail. By the way, ok, i understand that redirect is GET.
Hi @ledahu Could you find a solution?
Hi
i have a middleware `<?php
namespace App\Middleware;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use Slim\Views\Twig; use Symfony\Component\HttpFoundation\Session\Session;
final class OldInputMiddleware implements MiddlewareInterface {
}`
when i submit a form POST , the dump is empty , the getMethod is "GET"
`<?php
use App\Middleware\OldInputMiddleware; use App\Middleware\SessionMiddleware; use App\Middleware\UrlGeneratorMiddleware; use App\Middleware\ValidationErrorsMiddleware; use Selective\BasePath\BasePathMiddleware; use Selective\Validation\Middleware\ValidationExceptionMiddleware; use Slim\App; use Slim\Csrf\Guard; use Slim\Middleware\ErrorMiddleware; use Slim\Views\TwigMiddleware;
return function (App $app) {
$app->add(ValidationExceptionMiddleware::class); $app->add(Guard::class); $app->add(UrlGeneratorMiddleware::class);
};`
any idea ?