0

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

enter image description here

How should I use laravel validator if value is being sent as string in request?

4
  • We can't help you debug this without actually seeing your code. However I would guess that you're just sending the value as returned from val(), which is a string. Try using parseInt() on it to force the type coercion Commented Feb 1, 2020 at 17:46
  • I tried using parseInt() still values are being sent as string Commented Feb 1, 2020 at 18:01
  • Are you certain that the value being sent is "0", I just did a test and "0" passed the required validation. try dd($request->all()) before your validation to see what exactly is being passed Commented Feb 1, 2020 at 18:17
  • Yes I am sure it's string. I have updated the question with request values. Commented Feb 2, 2020 at 4:45

3 Answers 3

1

Any data sent over the internet is sent as string, and the destination parses and decodes this string the way it needs. That's why HTTP request parameters are always string.

When you receive a request and wait for a numeric value for a parameter, just convert it to number before using it. In your case, you are using Laravel PHP, you can use intval()

For Laravel validator, there's a validation rule to check if the incoming parameter is a number or not, which is numeric. Laravel knows that the parameter is string and deals with that. The validator doesn't check the data type, but it checks that the value of the string is a number.

You can find the full set of Laravel validation rules here: Laravel Validation Rules

Sign up to request clarification or add additional context in comments.

1 Comment

You pointed out correctly and I posted the answer with exact validation I needed.
1

You have 2 options:

  • Use parseInt and send the data as number. Though technically the request body is sent as string, your model will see the value in it as number and will treat it like that
  • Change your model on backend to parse the received string as number. But this is not recommended.

Comments

1

I added another laravel validation i.e not_in:0 and that worked for me. Simply its check for request parameter if zero whether parameter is string or integer

$validators = Validator::make($request->all(), [
            'lob_id' => 'required|not_in:0',
            'sub_lob_id' => 'required|not_in:0',
            'product_id' => 'required|not_in:0',
        ]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.