Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JackJones's avatar

Laravel boolean validation fails unless it equals "1"

I have the following JS:

    $.ajax({
        url: $(this).attr('action'),
        method: "POST",
        data: {
            "star_split": true
        }
    })

and that fails, but:

    $.ajax({
        url: $(this).attr('action'),
        method: "POST",
        data: {
            "star_split": 1
        }
    })

Passes

I ultimately want to use $("input[name='star_split']").is(':checked'), but "true" fails so do I have to do a conversion to an integer every time?

0 likes
11 replies
click's avatar

and that fails,

What fails exactly? The validation on your server? The request it self (500?)

JackJones's avatar

The validation says that star_split isn't a boolean, I suspect that the string "true" is being sent

click's avatar

Are you sure your first test is failing? In your first example you are sending it as a boolean. A normal html form POST request does not know boolean or integer values. Everything is a string. I assume that is also happening in your case. You should set the type of request you want to make. See https://stackoverflow.com/questions/3168401/json-formatting-sending-json-via-jquery-ajax-post-to-java-wicket-server

$.ajax({
    type: 'post',
    data: JSON.stringify({
            "star_split": true
        }),
    contentType: 'application/json',
    dataType: 'json'
});

I would recommend to debug this by dumping the data with dd(request()->post()) at the top of your controller method. Than you easily see how it receives the data.

JackJones's avatar

It definitely fails, in the end I just used

"star_split": $("input[name='star_split']").is(':checked') == true ? 1 : 0

Then it passes validation

D9705996's avatar

@JackJones - you should be able to just set the casts property on your model

protected $casts = [
    'star_split' => 'boolean',
];

Laravel will handle the conversion for you.

click's avatar

@D9705996 that isn't the problem here I think. The issue is that he does a an ajax request to the server and passes a boolean value. But that boolean value is not seen as boolean value on the server. So the request validation fails. The model isn't even hit here.

At least, that is what I understood.

JackJones's avatar

Yea that’s correct, it’s that I think the ajax request is converting true to a string

click's avatar

Read my earlier comment. I think the solution is to define the type of request for your ajax calls.

Cronix's avatar

Yes, you need to send an actual value, not a boolean when making a request. Just like you can't actually sent true/false in a normal form field without them being a string "true"/"false".

data: {
    "star_split": $("input[name='star_split']").is(':checked') ? 1 : 0;
}
D9705996's avatar

You need to make sure jQuery is actually passing a true/false value. If the other options are not working for you you can try passing in a Number object of your Boolean- https://www.w3schools.com/js/js_type_conversion.asp

$.ajax({
        url: $(this).attr('action'),
        method: "POST",
        data: {
            "star_split": Number(true)       
       }
    })

Your validation rule should be if its not already ( I didn't see this above)

star_split: boolean

The field under validation must be able to be cast as a boolean. Accepted input are true, false, 1, 0, "1", and "0".

https://laravel.com/docs/5.7/validation#rule-boolean

My previous cast comment ensures that eloquent always handles this field as a Boolean.

I would highly recommend looking at https://github.com/axios/axios in favour of jQuery for you Ajax requests. It handles all of the above for You and is the default package with laravel/vue and super readable!

axios.post($(this).attr('action'),{
  star_split": true     
}

Please or to participate in this conversation.