【问题标题】:Radio Button Returns 0 Boolean Value Every Time Laravel 8 Bootstrap单选按钮每次返回 0 布尔值 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'],
    ]);
}

提前致谢

【问题讨论】:

    标签: php html mysql laravel twitter-bootstrap


    【解决方案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 */
        ];
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-22
      • 2012-12-07
      • 2014-01-20
      相关资源
      最近更新 更多