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

[11.x] Ability to not run defer on validation errors #52890

Open
wants to merge 4 commits into
base: 11.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ public function handle(Request $request, Closure $next)
*/
public function terminate(Request $request, Response $response)
{
$hasErrors = $request->hasSession() && $request->session()->has('errors');
Container::getInstance()
->make(DeferredCallbackCollection::class)
->invokeWhen(fn ($callback) => $response->getStatusCode() < 400 || $callback->always);
->invokeWhen(fn ($callback) => ($response->getStatusCode() < 400 || $callback->always)
&& (! $hasErrors || $callback->evenWithErrors));
}
}
4 changes: 2 additions & 2 deletions src/Illuminate/Foundation/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,9 @@ function decrypt($value, $unserialize = true)
* @param bool $always
* @return \Illuminate\Foundation\Defer\DeferredCallback
*/
function defer(?callable $callback = null, ?string $name = null, bool $always = false)
function defer(?callable $callback = null, ?string $name = null, bool $always = false, $evenWithErrors = true)
{
return \Illuminate\Support\defer($callback, $name, $always);
return \Illuminate\Support\defer($callback, $name, $always, $evenWithErrors);
}
}

Expand Down
21 changes: 19 additions & 2 deletions src/Illuminate/Support/Defer/DeferredCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ class DeferredCallback
* @param callable $callback
* @return void
*/
public function __construct(public $callback, public ?string $name = null, public bool $always = false)
{
public function __construct(
public $callback,
public ?string $name = null,
public bool $always = false,
public bool $evenWithErrors = true,
) {
$this->name = $name ?? (string) Str::uuid();
}

Expand Down Expand Up @@ -43,6 +47,19 @@ public function always(bool $always = true): self
return $this;
}

/**
* Indicate that the deferred callback should run even on unsuccessful requests validations.
*
* @param bool $evenWithErrors
* @return $this
*/
public function evenWithErrors(bool $evenWithErrors = true): self
{
$this->evenWithErrors = $evenWithErrors;

return $this;
}

/**
* Invoke the deferred callback.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Support/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
* @param bool $always
* @return \Illuminate\Support\Defer\DeferredCallback
*/
function defer(?callable $callback = null, ?string $name = null, bool $always = false)
function defer(?callable $callback = null, ?string $name = null, bool $always = false, $evenWithErrors = true)
{
if ($callback === null) {
return app(DeferredCallbackCollection::class);
}

return tap(
new DeferredCallback($callback, $name, $always),
new DeferredCallback($callback, $name, $always, $evenWithErrors),
fn ($deferred) => app(DeferredCallbackCollection::class)[] = $deferred
);
}
Expand Down