Php Laravel扩展刀片模板-在$errors集合中出错

Php Laravel扩展刀片模板-在$errors集合中出错,php,laravel,templates,blade,Php,Laravel,Templates,Blade,我使用的是Laravel 5.3。我的第一次Laravel项目/学习经历 在我的blade文件中,我使用以下代码片段显示PUT或POST请求后字段下方的错误 在这种情况下,数据库字段称为firstName @if ($errors->has('firstName')) <span class="help-block"> <strong>{{ $errors->first('firstNam

我使用的是Laravel 5.3。我的第一次Laravel项目/学习经历 在我的blade文件中,我使用以下代码片段显示PUT或POST请求后字段下方的错误

在这种情况下,数据库字段称为firstName

        @if ($errors->has('firstName'))
            <span class="help-block">
                <strong>{{ $errors->first('firstName') }}</strong>
            </span>
        @endif
现在,由于我有很多字段,我一直在为每个字段重复这个块。我在Blade templates Extending Blade部分查找了Laravel文档,并认为可以在AppServiceProvider类AppServiceProvider.php中执行以下操作

public function boot()
{
    //
    Blade::directive('showIfError', function($fieldName) {
        if ($errors->has('$fieldName')) {
            echo "<span class='help-block'>
            <strong> $errors->first('$fieldName') </strong>
            </span>";
        }
    });
}
然后使用

@showIfError'firstName'

但没有运气…我得到了错误“未定义变量:错误”

似乎无法在此视图文件中访问Laravel错误集合

谢谢你的帮助。谢谢。

问题是$errors在闭包中不可访问。此外,您不能传递整个对象,因为指令闭包只接受字符串。对于简单的数据,您可以先内爆,然后将其分解,但不能使用对象或集合

您可以做的是在闭包内手动创建$errors

我已经对它进行了测试,它的工作原理与预期相符:

Blade::directive('showIfError', function($fieldName) {
    $errors = session('errors');

    if ($errors->has($fieldName)) {
        return "<span class='help-block'>".$errors->first($fieldName)."</span>";
    }
});

问题是$errors变量仅在视图中可用。如果查看共享变量的中间件,您将看到它存储在会话中

因此,您可以按如下方式访问它:

$errors = session()->get('errors');
请注意,在您的示例中,您确实存在一些其他问题;$fieldName变量不应在引号中。例如:

public function boot() {
Blade::directive('showIfError', function($fieldName) {
$errors = session()->get('errors');
 if ($errors->has($fieldName)) {
      echo "<span class='help-block'> <strong>". $errors->first($fieldName). "</strong> </span>";
 } 
});
}

最后,我在视图中编写了一个PHP函数,并使用各种字段名调用它。 我希望这是一个好办法。不确定实现这一点的最佳方式是什么

function showIfError($fieldName)
{
    $errors=session('errors');
    if ( count( $errors)>0) {
        if (session('errors')->has($fieldName)) {
            $msg=$errors->first($fieldName);
            echo '<span class="help-block">
                    <strong>'. $msg.' </strong>
                </span>';
        }
    }

}

这是迟来的答复,但希望它能帮助另一个来的人。自定义blade指令应该返回一个字符串php代码,该代码将在呈现模板时进行计算。由于$errors变量仅在做出响应时可用,因此在指令中尝试对其求值是无效的。解决办法是:

 // custom blade directive to render the error block if input has error
 // put this inside your service provider's boot method

     \Blade::directive('errorBlock', function ($input) {
            return
                '<?php if($errors->has('.$input.')):?>
                    <div class=\'form-control-feedback\'>
                        <i class=\'icon-cancel-circle2\'></i>
                    </div>
                    <span class=\'help-block\'>
                            <strong><?php echo $errors->first('.$input.') ?></strong>
                    </span>
                 <?php endif;?>';
        });

你能包括你想要实现的吗?我不想复制和粘贴如果错误。。。为表单中的每个字段设置块。相反,我想创建一个宏/刀片模板,如@showIfError'fieldName',最终输出将像上面给出的span块一样呈现。我发现视图缓存干扰了输出。如果我删除存储缓存文件夹中的所有文件,我可以使用会话“错误”使其工作,但它只能工作一次!再次提交带有相同错误的表单不会产生任何输出!因此,我无法完全测试任何答案是否有效:-此外,Blade::directive函数不接受2个参数。