I am sending select element selected value through ajax to Laravel controller action and validated the value as required in controller.
The problem is I am getting select element value as string not integer. So when first option is selected which has value 0, it goes as a string ("0") to controller and does not validated as required in action method.
The options to the select element is added dynamically.
Here is my code
HTML
var row_data = {
'lob_id': lob_id,
'sub_lob_id': sub_lob_id,
'product_id': product_id,
};
$.ajax({
url: '{{route('
getProductThreshold ')}}',
type: 'get',
dataType: 'json',
data: row_data,
success: function (response) {
loader(true);
console.log(response);
}
});
Laravel Controller
$validators = Validator::make($request->all(), [
'lob_id' => 'required',
'sub_lob_id' => 'required',
'product_id' => 'required',
]);
Controller Request
How should I use laravel validator if value is being sent as string in request?

val(), which is a string. Try usingparseInt()on it to force the type coercionparseInt()still values are being sent as string"0", I just did a test and"0"passed therequiredvalidation. trydd($request->all())before your validation to see what exactly is being passed