18910140161

php-单选按钮每次Laravel 8引导堆栈溢出时返回0布尔值

顺晟科技

2022-10-19 11:51:46

18

你好, 下面的代码段旨在询问用户是否想成为我们博客网站的客座作者,但是radio切换总是返回零值。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”的方式:

                    <!-- 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>

提前感谢


顺晟科技:

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

这样您就可以使用“1”为true,“0”为false来验证单选按钮, 新HTML代码,

                    <!-- 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>

我通过添加到数组中的模型来解决这个问题:

                    <!-- 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>

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

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航