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

Blade errors directive and route helper #27944

Closed
wants to merge 2 commits into from
Closed
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
3 changes: 2 additions & 1 deletion src/Illuminate/View/Compilers/BladeCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ class BladeCompiler extends Compiler implements CompilerInterface
Concerns\CompilesLoops,
Concerns\CompilesRawPhp,
Concerns\CompilesStacks,
Concerns\CompilesTranslations;
Concerns\CompilesTranslations,
Concerns\CompilesValidations;

/**
* All of the registered extensions.
Expand Down
11 changes: 11 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,15 @@ protected function compileMethod($method)
{
return "<?php echo method_field{$method}; ?>";
}

/**
* Compile the route statements into valid PHP.
*
* @param string $arguments
* @return string
*/
protected function compileRoute($arguments)
{
return "<?php echo route{$arguments}; ?>";
}
}
58 changes: 58 additions & 0 deletions src/Illuminate/View/Compilers/Concerns/CompilesValidations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Illuminate\View\Compilers\Concerns;

trait CompilesValidations
{
/**
* Compile the if-errorshas statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileErrorshas($expression)
{
return "<?php if(\$errors->has{$expression}): ?>";
}

/**
* Compile the end-errorshas statements into valid PHP.
*
* @return string
*/
protected function compileEndErrorshas()
{
return '<?php endif; ?>';
}

/**
* Compile the if-errorsany statements into valid PHP.
*
* @param string $expression
* @return string
*/
protected function compileErrorsany($expression)
{
return "<?php if(\$errors->hasAny{$expression}): ?>";
}

/**
* Compile the end-errorsany statements into valid PHP.
*
* @return string
*/
protected function compileEndErrorsany()
{
return '<?php endif; ?>';
}

/**
* Compile the errorsfirst statements into valid PHP.
*
* @return string
*/
protected function compileErrorsfirst()
{
return '<?php echo $errors->first(\'email\'); ?>';
}
}
19 changes: 19 additions & 0 deletions tests/View/Blade/BladeErrorsanyStatementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Illuminate\Tests\View\Blade;

class BladeErrorsanyStatementTest extends AbstractBladeTestCase
{
public function testErrorsanyAreCompiled()
{
$statement = '@errorsany([\'password\', \'email\'])
foo bar
@enderrorshas';

$expected = '<?php if($errors->hasAny([\'password\', \'email\'])): ?>
foo bar
<?php endif; ?>';

$this->assertEquals($expected, $this->compiler->compileString($statement));
}
}
11 changes: 11 additions & 0 deletions tests/View/Blade/BladeErrorsfirstStatementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Illuminate\Tests\View\Blade;

class BladeErrorsfirstStatementTest extends AbstractBladeTestCase
{
public function testErrorsfirstAreCompiled()
{
$this->assertEquals('<?php echo $errors->first(\'email\'); ?>', $this->compiler->compileString('@errorsfirst(\'email\')'));
}
}
32 changes: 32 additions & 0 deletions tests/View/Blade/BladeErrorshasStatementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Illuminate\Tests\View\Blade;

class BladeErrorshasStatementTest extends AbstractBladeTestCase
{
public function testErrorshasAreCompiled()
{
$statement = '@errorshas(\'password\')
foo bar
@enderrorshas';

$expected = '<?php if($errors->has(\'password\')): ?>
foo bar
<?php endif; ?>';

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

public function testErrorshasWithArrayAreCompiled()
{
$statement = '@errorshas([\'password\', \'email\'])
foo bar
@enderrorshas';

$expected = '<?php if($errors->has([\'password\', \'email\'])): ?>
foo bar
<?php endif; ?>';

$this->assertEquals($expected, $this->compiler->compileString($statement));
}
}
1 change: 1 addition & 0 deletions tests/View/Blade/BladeHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public function testEchosAreCompiled()
$this->assertEquals('<?php dd($var1); ?>', $this->compiler->compileString('@dd($var1)'));
$this->assertEquals('<?php dd($var1, $var2); ?>', $this->compiler->compileString('@dd($var1, $var2)'));
$this->assertEquals('<?php dump($var1, $var2); ?>', $this->compiler->compileString('@dump($var1, $var2)'));
$this->assertEquals('<?php echo route(\'users.store\'); ?>', $this->compiler->compileString('@route(\'users.store\')'));
}
}