John Davidson

php - Radio Button Returns 0 Boolean Value Every Time Laravel 8 Bootstrap

0 comments
Message:


Hello,
The following code segment is intended to ask the user if they want to be a guest author at our blogging site, however the radio toggle always returns a zero value. The MYSQL column is type tinyint(1).


                    <!-- Author? -->

<div class="author" style="text-align: center; margin-bottom: 1rem;">
<h3><strong>Are you interested in earning money blogging for us?</strong></h3>
<div class="form-check-inline">
<input class="form-check-input" name="is_author" type="radio" value="true" id="is_author1" style="top: 0.1rem; width: 1.50rem; height: 1.50rem;">
<h4 class="form-check-label" for="is_author">Yes, please.</h4>
</div>

<div class="form-check-inline">
<input class="form-check-input" name="is_author" type="radio" value="false" id="is_author0" style="top: 0.1rem; width: 1.50rem; height: 1.50rem;">
<h4 class="form-check-label" for="is_author">No, thank you.</h4>
</div>
</div>

And here is how 'is_author' is referenced in the controller:


protected function validator(array $data)
{
return Validator::make($data, [
'is_author' => ['required', 'boolean'],
]);
}

protected function create(array $data)
{
return User::create([
'is_author' => $data['is_author'],
]);
}

Thanks in advance



My Answer:
In your HTML code, the value attribute of your radio buttons should be set to "1" for true and "0" for false, not "true" and "false". Here is the corrected code:

html



Are you interested in earning money blogging for us?














By setting the value attribute to "1" and "0", the radio buttons will return the correct boolean values when submitted.

Rate this post

5 of 5 based on 6358 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us