Skip to content

Commit

Permalink
Merge pull request #28062 from calebporzio/add-error-blade-directive
Browse files Browse the repository at this point in the history
[5.8] Add @error blade directive
  • Loading branch information
taylorotwell committed Apr 17, 2019
2 parents 91a6afe + e44d786 commit 41e158f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
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\CompilesErrors,
Concerns\CompilesHelpers,
Concerns\CompilesIncludes,
Concerns\CompilesInjections,
Expand Down
34 changes: 34 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesErrors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Illuminate\View\Compilers\Concerns;

trait CompilesErrors
{
/**
* 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));
}
}

0 comments on commit 41e158f

Please sign in to comment.