简体   繁体   English

Laravel Checkobx在构建表单中不会更新值

[英]Laravel checkobx in build form doesn't update value

In Laravel I'm using Form builder. 在Laravel中,我正在使用“表单生成器”。 My form is same for creating and updating post. 我的表单与创建和更新帖子相同。 In that form I create checkbox for enabling/disabling comments in post. 在该表格中,我创建了一个复选框,用于启用/禁用帖子中的评论。 Default value is 1 (checked). 默认值为1(选中)。 Checkbox works fine when I'm creating new post. 创建新帖子时,复选框可以正常工作。 Also when I load edit page, if in post from database is enabled comment, page load checked box and if in post is disabled comments page load unchecked box. 同样,当我加载编辑页面时,如果在数据库中的帖子中启用了注释,则页面加载复选框处于选中状态;如果在数据库中的帖子禁用,则注释页面加载未选中复选框。 Problem is when I want to edit and change this and update post. 问题是当我想编辑和更改它并更新帖子时。 Enable comments value not changing. 启用评论值不变。 There is no any error and changed values from other form fields are store correctly. 没有任何错误,并且其他表单字段中的更改值已正确存储。 Column name is enable_comments - boolean type and this is my form builder input field code: 列名称为enable_comments-布尔类型,这是我的表单构建器输入字段代码:

{!! Form::label('enable_comments') !!}
{!! Form::checkbox('enable_comments', $post->exists ? $post->enable_comments : 1, !$post->exists ? true : $post->enable_comments) !!}

Note, everything works fine on creating post. 请注意,创建帖子后一切正常。 If checkbox is checked, it store 1 in database, if it's not, it store 0. Also, it display correct on editing page. 如果选中此复选框,则它将1存储在数据库中;如果未选中,则存储0。此外,它在编辑页面上显示正确。 If "enable_comments" value in database is 1, it's checked on edit page, if it's 0, checkbox is unchecked. 如果数据库中的“ enable_comments”值为1,则在编辑页面上选中该复选框,如果为0,则未选中该复选框。

Please help. 请帮忙。 Where I'm making wrong? 我在哪里弄错了? Where is a problem in this logic? 这个逻辑在哪里出问题?

is this only happening when you uncheck the box? 仅当您取消选中此框时才会发生这种情况吗? When its unchecked it doesn't send a value back. 如果未选中,则不会发送回值。 Adding the below line to a Request class will default the value so it exists every time regardless if its checked 将以下行添加到Request类将默认该值,因此无论是否选中它,每次都存在

public function rules()
{
    $this->merge(['enable_comments' => $this->input('enable_comments', 0),]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM