简体   繁体   中英

Form validation in select field

Is there a way I can check if a user didn't manipulate the form? In my form, I get all available entries, but if a user changes the value id in the browser, I would just get an error. Any tips:)?

<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>

you can do select readonly

<select name="product_id" class="form-control" required readonly>

but user can change html by devtools

also you can check it on backend side like this

if ($products->pluck('id')->diff(request()->input('product_id'))->empty()) {
    //not changed
}

You could use a Rule to validate the received data like:

use Illuminate\Validation\Rule;

Validator::make($data, [
    'product_id' => [
        'required',
        Rule::in([/*array of products ids here*/]),
    ],
]);

Take a look at https://laravel.com/docs/5.8/validation#rule-in

You could use exists like:

Validator::make($data, [
    'product_id' => [
        'required',
        'exists:table_name,column_name'
    ],
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM