单选按钮每次返回 0 布尔值 Laravel 8 Bootstrap

Posted

技术标签:

【中文标题】单选按钮每次返回 0 布尔值 Laravel 8 Bootstrap【英文标题】:Radio Button Returns 0 Boolean Value Every Time Laravel 8 Bootstrap 【发布时间】:2021-11-01 10:08:54 【问题描述】:

你好, 以下代码段旨在询问用户是否想成为我们博客网站的客座作者,但是单选切换始终返回零值。 MYSQL 列的类型为 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>

下面是控制器中“is_author”的引用方式:

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'],
    ]);

提前致谢

【问题讨论】:

【参考方案1】:

您总是得到 0 的原因是您的单选按钮总是将输入值作为字符串类型发送。所以,它是“真实的”而不是真实的。因此,您不能使用布尔验证规则来验证“真”和“假”。因为它是一个字符串,验证规则总是会失败。 在 laravel 8 dock 中明确提到“接受的输入是 true、false、1、0、“1”和“0”。” (https://laravel.com/docs/8.x/validation#rule-boolean)

所以您可以使用“1”为真,“0”为假来验证您的单选按钮,没有问题, 新的 html 代码,

<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="1" 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="0" 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>

【讨论】:

如果我提交选择了“author1”的表单,这仍然返回 0。感谢您的回复。 你能回显 is_author 并向我显示@All_Semicolons 的值【参考方案2】:

我通过在$fillable 数组中的模型中添加'is_author' 解决了这个问题:

class User extends Authenticatable

    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $fillable = [
        'first_name',
        'last_name',
        'website',
        'email',
        'password',
        'is_author', /* <--- NEW ELEMENT */
    ];

以前,所有值都作为我在数据库中设置的默认值返回,该值为零。 希望这对将来的某人有所帮助。

【讨论】:

以上是关于单选按钮每次返回 0 布尔值 Laravel 8 Bootstrap的主要内容,如果未能解决你的问题,请参考以下文章

MUI 单选按钮组返回字符串,尽管它提供了布尔值

如何通过 Node + Express + Mongoose 中的单选按钮传递布尔值?

单击单选按钮或更改 PyQt 中的旋转框时,组合框索引值返回默认值 0

单选按钮的 knockout.js 布尔数据绑定问题

ReactJS:如何在单选按钮中使用布尔值?

Laravel 布尔值在查询中返回“1”/“0”而不是 true/false