[英]Form validation in select field
有沒有辦法檢查用戶是否沒有操作表單? 在我的表單中,我得到了所有可用的條目,但是如果用戶在瀏覽器中更改值 id,我只會得到一個錯誤。 有小費嗎:)?
<div class="form-group-row club col-lg-10">
<label>Choose Product</label>
<select name="product_id" class="form-control" required>
@foreach($products as $product)
<option value="{{$product->id}}">{{$product-> product}}</option>
@endforeach
</select>
</div>
你可以只讀 select
<select name="product_id" class="form-control" required readonly>
但用戶可以通過 devtools 更改 html
你也可以像這樣在后端檢查它
if ($products->pluck('id')->diff(request()->input('product_id'))->empty()) {
//not changed
}
您可以使用規則來驗證接收到的數據,例如:
use Illuminate\Validation\Rule;
Validator::make($data, [
'product_id' => [
'required',
Rule::in([/*array of products ids here*/]),
],
]);
看看https://laravel.com/docs/5.8/validation#rule-in
你可以像這樣使用存在:
Validator::make($data, [
'product_id' => [
'required',
'exists:table_name,column_name'
],
]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.