DUMP AND DIE ON FORM REQUEST
Jan 12, 2024 Copy Link
Laravel has a nice debugging tool which is the `dd`
method, Let's use this method in the Form Request
class to debug some actions so, let's create a fresh one:
php artisan make:request User/StoreRequest
Go ahead, crack open that class, and add the `after`
method in addition to the `rules`
default method:
use Closure; use Illuminate\Validation\Validator; /** * Indicates if the validator should stop on the first rule failure. * * @var bool */ protected $stopOnFirstFailure = true;
/** * Get the "after" validation callables for the request. */ public function after(): Closure { return function (Validator $validator) { if ($checkForSomething) { dd('Correct Condition!'); $validator->errors()->add('error', 'Do something!'); } dd('Validation Has Passed!'); }; }
According to the previous code, if the coming request fulfills the condition, Laravel will dump the text and then die on that line. After you have ensured that the validation is working you will remove the first `dd`
then you wait for the Do something!
error message to be shown, however, the Validation Has Stopped!
will be printed out but, if you remove the second dump and die
the message will be revealed successfully 🚀
Laravel will print the error messages out after fully executing this class and there is no hindrance to doing that.