-
-
Save niladam/94f504b9ef41f84fe7f4ad3e8cbfd147 to your computer and use it in GitHub Desktop.
A little Blade directive to make working with validation errors a bit nicer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Usage: | |
// Before | |
@if ($errors->has('email')) | |
<span>{{ $errors->first('email') }}</span> | |
@endif | |
// After: | |
@error('email') | |
<span>{{ $message }}</span> | |
@enderror | |
// Include the following in your "app/Providers/AppServiceProvider.php": | |
class AppServiceProvider extends ServiceProvider | |
{ | |
public function boot() | |
{ | |
Blade::directive('error', function ($expression) { | |
return <<<EOT | |
<?php | |
if (\$errors->has({$expression})) : | |
if (isset(\$message)) { \$messageCache = \$message; } | |
\$message = \$errors->first({$expression}); | |
?> | |
EOT; | |
}); | |
Blade::directive('enderror', function () { | |
return <<<EOT | |
<?php | |
unset(\$message); | |
if (isset(\$messageCache)) { \$message = \$messageCache; } | |
endif; | |
?> | |
EOT; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment