如何在默认情况下选中复选框并从laravel中的数据库中检索旧值 [英] How to make a checkbox checked by default and also retrieve old value from database in laravel

查看:37
本文介绍了如何在默认情况下选中复选框并从laravel中的数据库中检索旧值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复选框,并且我希望在默认情况下选中我的复选框,如果用户取消选中该复选框并保存表单,我需要在检索或编辑表单数据时取消选中该复选框。

我的视图页面

<div class="form-group {{ $errors->has('active') ? 'error' : '' }}">
                <label for="active" class="col-md-3 control-label">@lang('admin/assetdetails/form.active')</label>
                     <div class="controls col-md-7">
                            {{ Form::checkbox('active', 1, Input::old('active', $assetdetail->active), array('class'=>'isnesdbox')) }}
                            {{ $errors->first('active', '<span class="alert-msg"><i class="icon-remove-sign"></i> :message</span>') }}
                     </div>
            </div>

提供此选项可从数据库正确返回旧数据,但我无法将复选框选为默认

控制器:

public function postEdit($assetdetailId = null)
    {
        // Check if the location exists
        if (is_null($assetdetail = Assetdetail::find($assetdetailId)))
        {
            // Redirect to the blogs management page
            return Redirect::to('admin/settings/assetdetails')->with('error', Lang::get('admin/assetdetails/message.does_not_exist'));
        }



        // get the POST data
        $new = Input::all();

        // attempt validation
        if ($assetdetail->validate($new))
        {

            // Update the location data

            $assetdetail ->asset_number             = e(Input::get('asset_number'));
            $assetdetail ->location_id              = e(Input::get('location_id'));
            $assetdetail ->assign_to                = e(Input::get('assign_to'));
            $assetdetail ->asset_type_id            = e(Input::get('asset_type_id'));
            $assetdetail ->nesd                     = e(Input::get('nesd'));
            $assetdetail ->active                   = e(Input::get('active'));
            $assetdetail ->shift                    = e(Input::get('shift'));
            $assetdetail ->supplier_name            = e(Input::get('supplier_name'));
            $assetdetail ->description              = e(Input::get('description'));
            $assetdetail ->dateof_purchase          = e(Input::get('dateof_purchase'));
            $assetdetail ->label_number             = e(Input::get('label_number'));
            $assetdetail ->purchase_price           = e(Input::get('purchase_price'));
            $assetdetail ->dateof_disposed          = e(Input::get('dateof_disposed'));
            $assetdetail ->depreciation_type        = e(Input::get('depreciation_type'));
            $assetdetail ->salvage_value            = e(Input::get('salvage_value'));
            $assetdetail ->asset_life               = e(Input::get('asset_life'));




            // Was the asset created?
            if($assetdetail->save())
            {
                // Redirect to the saved location page
                return Redirect::to("admin/settings/assetdetails/$assetdetailId/edit")->with('success', Lang::get('admin/assetdetails/message.update.success'));
            }
        }
        else
        {
            // failure
            $errors = $assetdetail->errors();
            return Redirect::back()->withInput()->withErrors($errors);
        }

        // Redirect to the location management page
        return Redirect::to("admin/settings/assetdetails/$assetdetailId/edit")->with('error', Lang::get('admin/assetdetails/message.update.error'));

    }

我已尝试

{{ Form::checkbox('active', 1, true, Input::old('active', $assetdetail->active), array('class'=>'isnesdbox')) }}

但我得到以下错误

不能将标量值用作数组

我也尝试过

<input class="col-md-1 controls isnesdbox" type="checkbox" name="active" checked id="active" value="1" {{ $assetdetail->active === '1' ? 'checked' : '' }} />

请帮助我实现这一点 注意:IAM使用的是MySQL数据库,IAM使用的数据库类型是BIT,它根据用户输入存储0和1。

推荐答案

听起来您并不真正关心$assetDetail->Active的值是什么,只是希望选中该复选框,直到用户取消选中它。

这是否如您所愿:

{{ Form::checkbox('active', 1, Session::hasOldInput() ? array_key_exists('active', Input::old()) : true, array('class'=>'isnesdbox')) }}

还是我误读了您要做的事情?

对于此解决方案,它检查会话中是否有旧输入。如果有旧的输入数据,它使用'active'键的存在来确定复选框[1]的状态。如果没有旧的输入数据,则默认为选中该复选框。

[1]复选框提交的方式与其他表单元素不同。未选中的复选框不被视为"成功"控件,因此它不会提交任何数据,因此不会存在于Input::old()数组中。因此,如果键存在,则检查它;如果键不存在,则不检查它。您可以阅读有关"成功"表单控件的更多信息here


编辑

根据注释,看起来创建和编辑AssetDetail对象使用了相同的表单。在创建时,您希望默认选中该复选框,在编辑时,您希望该复选框反映存储在数据库中的值。

我在上面发布的代码应该很接近,但您需要更改三元运算符中的硬编码true

如果$assetDetail在创建期间为NULL,您将需要:

{{ Form::checkbox('active', 1, Session::hasOldInput() ? array_key_exists('active', Input::old()) : (!empty($assetdetail) ? $assetdetail->active : true), array('class'=>'isnesdbox')) }}

如果$assetDetail在创建过程中是一个AssetDetail实例,您将需要:

{{ Form::checkbox('active', 1, Session::hasOldInput() ? array_key_exists('active', Input::old()) : ($assetdetail->exists ? $assetdetail->active : true), array('class'=>'isnesdbox')) }}

这篇关于如何在默认情况下选中复选框并从laravel中的数据库中检索旧值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆