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

haxxorsid's avatar

Validation rule boolean: The field must be true or false.

JS (it's a checkbox):

var show = $("input[name=show]").prop('checked')

Console.log correctly gives true/false.

Laravel:

$this->validate($request, [
    'show' => 'required|boolean'
]);

Error: The show field must be true or false.

I have tried this:

var show = ($("input[name=show]").prop('checked') === 'true'); // also == instead of ===

Still it doesn't work.

0 likes
2 replies
Cronix's avatar
Cronix
Best Answer
Level 67

Try let show = $("input[name='show']").is(":checked") ? 1 : 0;

I don't think you can return 'true' in quotes, since that's a string and not an actual boolean value. I think the named attribute also needs to be quoted. If you check the docs for boolean rule, it states:

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

1 like

Please or to participate in this conversation.