【问题标题】:Laravel components with old() helper function具有 old() 辅助函数的 Laravel 组件
【发布时间】:2020-08-14 21:54:57
【问题描述】:

为方便起见,我制作了一个包含下拉列表的组件。这样我就可以在需要时在任何地方使用它。这是组件

 <h5>{{$title}}</h5>

    <select class="select" {{$required_district}} name="{{$select_district}}" id="{{$select_district}}">

        <option value="" selected> {{ __('Select the district') }} </option>

        @foreach($districts as $district)
            <option value="{{ $district->id }}"
               {{ old($select_district) && $district->id == old($select_district)  ? 'selected' : '' }}>
                {{ $district->name_en }}</option>
        @endforeach

    </select>

    @error($select_district)
    <label class="text-danger" style="">{{$message}}</label>
    @enderror

我在这里做的是标题、输入文件名 id 和其他类型的东西通过@slot() 动态生成,如下所示

    @component('destrict')

 @slot('title')
 Where do you want a job to be started
 @endslot

 @slot('select_district')
 selected_district
 @endslot

 @slot('required_district')
 required
 @endslot

    @endcomponent

好的,但当我提交包含此组件的表单时,验证失败时,它会生成以下错误。

array_key_exists():第一个参数应该是字符串或 整数

当表单重定向返回并出现验证错误时,此行会出现问题

 {{ old($select_district) && $district->id == old($select_district)  ? 'selected' : '' }}

如果我删除 {{ old($select_district) }} 或者我只是像 {{ old('name_here') }} 一样使用它,那么一切都会正常运行。但是因为我需要在更多页面中使用这个组件,所以我想解决这个问题。所以谁能帮我解决这个问题。

【问题讨论】:

    标签: php laravel forms validation


    【解决方案1】:

    这可能很棘手,但我能够修复如下错误

    old(''.$select_district.'')
    

    谢谢

    【讨论】:

      猜你喜欢
      • 2020-07-30
      • 2020-08-04
      • 2022-08-04
      • 2015-12-07
      • 2016-01-06
      • 1970-01-01
      • 2015-04-13
      • 2014-04-19
      • 1970-01-01
      相关资源
      最近更新 更多