我在laravel 5.4中有一个表格,允许人们创建问卷 - 动态添加问题字段(这在js的其他地方处理)。
<form method="POST" action="/questions" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label for="featured-img">Featured Image</label>
<input type="file" accept="image/*" class="featured-image image-uploader" id="featured-img" name="featured_img" />
<img src="" class="image-preview" alt="uploaded image preview" />
</div>
<div class="form-group">
<label for="title">Title</label>
<input name="title" type="text" class="form-control" id="title" placeholder="Title" required>
</div>
<div class="form-group">
<label for="description">Description</label>
<input name="description" type="text" class="form-control" id="description" placeholder="Description" required>
</div>
<div class="form-control">
<label for="skill_level">Skill Level</label>
<select name="skill_level" class="custom-select" id="skill_level" required>
<option value="kids">Kids</option>
<option value="beginner">Beginner</option>
<option value="intermediate">Intermediate</option>
<option value="advanced">Advanced</option>
</select>
</div>
<div class="add-contributor-container">
<button id="add-contributor">Add contributor</button>
</div>
<div id="contributor-section">
<div class="form-group contributors">
<label for="contributors-1">Contributor 1</label>
<input name="contributors[1]" class="form-control contributor-name" id="contributors-1" required />
<input name="contributors_amount[1]" class="form-control contributor-amount" id="contributors-amount-1" required />
<button type="button" class="remove-contributor">Remove Contributor</button>
</div>
</div>
<div class="form-group">
<label for="category">Category</label>
<select name="category_id" class="form-control" id="category" required>
@foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->title }}</option>
@endforeach
</select>
</div>
<div class="add-question-container">
<button type="button" id="add-question">Add question</button>
</div>
<div id="question-section">
<div class="form-group question">
<label for="questions-1">Question 1</label>
<textarea name="questions[1]" class="form-control question-content" id="questions-1" rows="3" required></textarea>
<input type="file" accept="image/*" class="questions-image image-uploader" name="questions_img[1]" />
<img src="" class="image-preview" alt="uploaded image preview" />
<button type="button" class="remove-question">Remove Question</button>
</div>
</div>
<div class="form-group">
<label for="published">Publish</label>
<p>If unticked, only you will be able to see this project. You can edit this later.</p>
<input name="published" type="checkbox" class="form-control" id="published" value="1" checked="checked">
</div>
<input type="hidden" value="1" name="order" />
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
当我在我的控制器中检索请求时,它包含我的所有表单数据(包括贡献者数组,所以我假设我的输入数组语法是正确的)但缺少questions_img数组。
对于为什么只有这个字段不会被发送,我有什么遗漏? 以下是我在控制器中检查请求的方法:
public function store( StoreProject $request )
{
dd( $request );
}
而StoreProject是我创建的FormRequest(可能是主要罪魁祸首):
namespace App\Http\Requests;
使用Illuminate \ Foundation \ Http \ FormRequest;
类StoreProject扩展了FormRequest { / ** *确定用户是否有权提出此请求。 * * @return bool * / 公共职能授权() { 返回true; }
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$rules = [
'title' => 'required',
'category_id' => 'required',
'description' => 'required',
'order' => 'required',
'featured_img' => 'image',
'skill_level' => 'required|string'
];
foreach( $this->request->get( 'questions_img' ) as $key => $val )
{
$rules['questions_img.'.$key] = 'image';
}
return $rules;
}
}
总结一下:如果我在商店功能中使用正常的请求而不是 StoreProject ,并调用 $ request-&gt; all( )该字段正确传递。如果我使用自定义表单请求,有没有理由为$ request缺少字段?