简体   繁体   中英

Laravel checkobx in build form doesn't update value

In Laravel I'm using Form builder. 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). 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:

{!! 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. If "enable_comments" value in database is 1, it's checked on edit page, if it's 0, checkbox is unchecked.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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