我在 Laravel 中工作,遇到了一个我不明白的问题,我有这个组件,它使用递归从存储在表中的 json 构建整个表单:
@foreach($data as $index => $field)
@isset($lastIndex)
@php
$name = $template->slug."%%".$field['key']."%%".$lastIndex
@endphp
@endisset
@if($field['type'] == 'object')
<div class="card border card-border-light justify-content-center">
<div class="card-body">
<div class="row mb-3">
<div class="col-lg-2">
<label class="form-label">{{$field['label']}}</label>
</div>
<div class="col-lg-10">
@component('components.templates.templateForm', [
'template' => $template,
'data' => $field['data'],
'disabled' => $disabled,
'lastIndex' => $field['key'],
])
@endcomponent
</div>
</div>
</div>
</div>
@elseif($field['type'] == 'string')
<div class="row mb-3">
<div class="col-lg-2">
<label for="hero_title" class="form-label">{{$field['label']}}</label>
</div>
<div class="col-lg-10">
<input name="{{$name ?? $template->slug."%%".$field['key']}}" class="form-control"
value="{{$name ?? $template->slug."%%".$field['key']}}" id="hero_title"
placeholder="Titulo do banner" {{$disabled ? 'disabled' : ''}}>
</div>
</div>
@elseif($field['type'] == 'text')
<div class="row mb-3">
<div class="col-lg-2">
<label for="hero_title" class="form-label">{{$field['label']}}</label>
</div>
<div class="col-lg-10">
<textarea name="{{$name ?? $template->slug."%%".$field['key']}}" class="form-control"
id="hero_title"
placeholder="Titulo do banner" {{$disabled ? 'disabled' : ''}}>{{$name ?? $template->slug."%%".$field['key']}}</textarea>
</div>
</div>
@elseif($field['type'] == 'image')
<div class="row mb-3">
<div class="col-lg-2">
<label for="hero_title" class="form-label">{{$field['label']}}</label>
</div>
<div class="col-lg-10">
<input name="{{$name ?? $template->slug."%%".$field['key']}}" class="form-control"
id="hero_title" value="{{$name ?? $template->slug."%%".$field['key']}}"
placeholder="Titulo do banner" {{$disabled ? 'disabled' : ''}} >
<div class="row mb-3" style='margin:10px;'>
<div class="col-lg-2">
<label for="hero_title" class="form-label">Preview:</label>
</div>
<div class="col-lg-10">
<img
src="https://site-.s3.sa-east-1.amazonaws.com/wp-content/uploads/sites/5/2023/06/19134118/logo-unicamp.png"
alt='image' class="images-table-list">
</div>
</div>
</div>
</div>
@endif
@endforeach
然后我使用这个组件作为我的表单的输入
<form action="{{ route('templates.store') }}" method="POST">
@csrf
@component('components.templates.templateForm', [
'template' => $template,
'data' => $data,
'disabled' => true,
])
@endcomponent
<button type="submit" class="btn btn-primary">Salvar</button>
</form>
但是,当我转到控制器上的 store 方法,并 dd() $request->all() 时,我只得到 @csrf 字段,没有其他任何人知道为什么?
直接在表单上放置输入可以解决它,但情况不应该如此,组件不是只是构建表单吗?我怀疑表单是在组件之前制作的,因此当组件完成构建输入时,表单已经自行制作,忘记了组件。但我真的不知道。
是的,我很愚蠢,问题是禁用字段如名称所示,被禁用,因此被表单忽略,抱歉在这方面浪费了 stackoverflow 内存。