I try to set up and test validation on slim.
after redirect and retrieve error message just fine.
status code 422 isn't set.
unless redirection is wrong.
if ($validation->failed()) {
return $response->withRedirect((string)$request->getUri())->withStatus(422);
}
if ($validation->failed()) {
return $response->withRedirect((string)$request->getUri(), 422);
}
md5-8ed088bd4c09311176c50f87d012d409
if ($validation->failed()) {
return $response->withHeader('Location', (string)$request->getUri())->withStatus(422);
}
any help many thanks.
What status code is received by your client? Have you tried one of the 300-series (by default they are responsible for redirects)? What server are you using?
well, the redirect code shown is 302 by default.
expect to see 422 for failed validation.
Try 307 instead of 422. Just to make sure that your server does not override the status code if it detects a Location header?
See for example https://gist.github.com/mttjohnson/e9b8b7b6466834f03ffb59bb4cad14f2
status code 307 give ERR_TOO_MANY_REDIRECTS
I do use Apache, not Nginx. does make any difference?
Okay, so the status code from Slim is working. I suspect that your server is overriding, when Location is present. Now, if you get "too many redirects", there is another problem with your code.
Basically you should use a 3xx status (or 201) in combination with Location.
image of browser.

invoke method
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $args): ResponseInterface
{
$validation = $this->validator->validate($request, ValidatorRules::login());
if ($validation->failed()) {
return $response
->withHeader('Location', (string)$request->getUri())
->withStatus(422);
}
return $response;
}
I did try with clean slim. and did the same thing didn't set status code
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\Factory\AppFactory;
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor\autoload.php';
$app = AppFactory::create();
$app->get('/', function (ServerRequestInterface $request, ResponseInterface $response, $args):ResponseInterface {
return $response->withHeader('Location', '/test')->withStatus(422);
});
$app->get('/test', function (ServerRequestInterface $request, ResponseInterface $response, $args):ResponseInterface {
return $response;
});
$app->run();
But the redirect is working? I mean the /test callback gets called in the second (redirected) request?
As I said, I guess that your Apache is overriding http status codes if it detects a Location header in combination with an "invalid" code. Perhaps you could add a Status header and read that one?
Why do you try to send a 422 http status code along with a redirect?
sorry about that thing 422 is only for API
I did light reading how to use status code
For web development, in my opinion, it is crucial to understand the http protocol and how headers (and status codes) can and should be used.
So this issue can be closed then?
yes thanks for help
Most helpful comment
Why do you try to send a
422http status code along with a redirect?