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

[5.8] Add @error blade directive #28062

Merged
merged 2 commits into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class BladeCompiler extends Compiler implements CompilerInterface
Concerns\CompilesComponents,
Concerns\CompilesConditionals,
Concerns\CompilesEchos,
Concerns\CompilesError,
Concerns\CompilesHelpers,
Concerns\CompilesIncludes,
Concerns\CompilesInjections,
Expand Down
34 changes: 34 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Illuminate\View\Compilers\Concerns;

trait CompilesError
{
/**
* Compile the error statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileError($expression)
{
$expression = $this->stripParentheses($expression);

return '<?php if ($errors->has('.$expression.')) :
if (isset($message)) { $messageCache = $message; }
$message = $errors->first('.$expression.'); ?>';
}

/**
* Compile the enderror statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileEnderror($expression)
{
return '<?php unset($message);
if (isset($messageCache)) { $message = $messageCache; }
endif; ?>';
}
}
24 changes: 24 additions & 0 deletions tests/View/Blade/BladeErrorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Illuminate\Tests\View\Blade;

class BladeErrorTest extends AbstractBladeTestCase
{
public function testErrorsAreCompiled()
{
$string = '
@error(\'email\')
<span>{{ $message }}</span>
@enderror';
$expected = '
<?php if ($errors->has(\'email\')) :
if (isset($message)) { $messageCache = $message; }
$message = $errors->first(\'email\'); ?>
<span><?php echo e($message); ?></span>
<?php unset($message);
if (isset($messageCache)) { $message = $messageCache; }
endif; ?>';

$this->assertEquals($expected, $this->compiler->compileString($string));
}
}