[英]Laravel FormRequest validation rules not working as expected
In my Laravel app, I am creating a dynamic search functionality using Eloquent and I have a customer search form which looks like this: 在我的Laravel应用中,我正在使用Eloquent创建动态搜索功能,并且有一个客户搜索表单,如下所示:
customers.search.blade.php Contrived Example customer.search.blade.php 人为的示例
<form method="post" action="{{ route('customers.search') }}">
@csrf
<div class="form-group">
<label for="first_name" class="font-weight-bold">First Name</label>
<div class="input-group">
<div class="input-group-prepend">
<select name="first_name['operator']" class="custom-select">
<option value="%">%</option>
<option value="=">=</option>
<option value="!=">!=</option>
</select>
</div>
<input id="first_name" name="first_name['query']" type="text" class="form-control">
</div>
</div>
</form>
I have a SearchCustomerRequest
(FormRequest) class which builds out the validation rules dynamically. 我有一个
SearchCustomerRequest
(FormRequest)类,该类可以动态构建验证规则。 I've dumped it, so you can see what the generated rules looks like: 我已将其丢弃,因此您可以看到生成的规则是什么样的:
In my CustomersController under search
method, I did the following to see what the validated request array looks like: 在我的CustomersController中的
search
方法下,我做了以下操作以查看经过验证的请求数组的外观:
class CustomersController extends Controller
{
// ...
public function search(SearchCustomerRequest $searchCustomerRequest)
{
dd($searchCustomerRequest->validated());
}
// ...
}
In order to test this, in the search form, I've selected the firstname['operator']
to %
and typed the first_name['query']
to test
and submitted the form. 为了对此进行测试,我在搜索表单中选择了
firstname['operator']
作为%
然后键入first_name['query']
进行test
并提交了表单。
I got the following response ( ie empty array ): 我得到以下响应( 即空数组 ):
[]
[]
So, I dumped the whole request object $searchCustomerRequest
to see what was in the parameter bag and this is what I see: 因此,我转储了整个请求对象
$searchCustomerRequest
来查看参数包中的内容,这就是我看到的内容:
As you can see, my request is valid and my rules also looks correct, yet the validation doesn't seem to be working as expected. 如您所见,我的请求是有效的,我的规则也看起来正确,但是验证似乎没有按预期工作。
Any ideas what might be wrong here? 有什么想法在这里可能出什么问题吗?
In your ParameterBag
, the first_name
properties operator
and query
are enclosed in single quotes. 在
ParameterBag
, first_name
属性operator
和query
用单引号引起来。 In your HTML, the name attribute should exclude the single quotes. 在HTML中,name属性应排除单引号。 Ex:
name="first_name[query]"
例如:
name="first_name[query]"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.