meg*_*age 3 php checkbox laravel laravel-4
我有一个复选框,我想让我的复选框默认选中,如果用户取消选中复选框并保存表单,我需要在检索或编辑表单数据时取消选中该复选框。
我的查看页面
<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>
Run Code Online (Sandbox Code Playgroud)
给这个正确地从数据库中带回旧数据,但我无法将我的复选框选中为默认值
控制器:
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'));
}
Run Code Online (Sandbox Code Playgroud)
我试过
{{ Form::checkbox('active', 1, true, Input::old('active', $assetdetail->active), array('class'=>'isnesdbox')) }}
Run Code Online (Sandbox Code Playgroud)
但我得到了以下错误
不能将标量值用作数组
我也试过这个
<input class="col-md-1 controls isnesdbox" type="checkbox" name="active" checked id="active" value="1" {{ $assetdetail->active === '1' ? 'checked' : '' }} />
Run Code Online (Sandbox Code Playgroud)
请帮我实现这个注意:iam 使用 mysql 数据库,iam 使用的数据库类型是 bit,它根据用户输入存储 0 和 1。
小智 9
由于谷歌显然仍然认为这个答案是相关的,这里是 Laravel 5.8 的更新答案:
<input type="checkbox" value="true" name="notify" @if(!old() || old('notify') == 'true') checked="checked" @endif />
Run Code Online (Sandbox Code Playgroud)
听起来您并不真正关心 $assetdetail->active 的值是多少,您只想选中复选框,直到用户取消选中它。
这是否符合您的要求:
{{ Form::checkbox('active', 1, Session::hasOldInput() ? array_key_exists('active', Input::old()) : true, array('class'=>'isnesdbox')) }}
Run Code Online (Sandbox Code Playgroud)
或者我误读了你想要做什么?
对于此解决方案,它会检查会话中是否存在旧输入。如果有旧的输入数据,它使用'active'
键的存在来确定复选框 [1] 的状态。如果没有旧的输入数据,则默认复选框为选中状态。
[1] 复选框提交与其他表单元素不同。未选中的复选框不被视为“成功”控件,因此它不会提交任何数据,因此不会存在于Input::old()
数组中。因此,如果密钥存在,则对其进行检查;如果密钥不存在,则未检查。您可以在此处阅读有关“成功”表单控件的更多信息。
根据评论,看起来创建和编辑 Assetdetail 对象使用了相同的表单。创建时,您希望复选框默认选中,编辑时您希望复选框反映存储在数据库中的值。
我在上面发布的代码应该很接近,但是您需要更改true
三元运算符中的硬编码。
如果 $assetdetail 在创建过程中为空,您需要:
{{ Form::checkbox('active', 1, Session::hasOldInput() ? array_key_exists('active', Input::old()) : (!empty($assetdetail) ? $assetdetail->active : true), array('class'=>'isnesdbox')) }}
Run Code Online (Sandbox Code Playgroud)
如果 $assetdetail 在创建过程中是一个 Assetdetail 实例,您将需要:
{{ Form::checkbox('active', 1, Session::hasOldInput() ? array_key_exists('active', Input::old()) : ($assetdetail->exists ? $assetdetail->active : true), array('class'=>'isnesdbox')) }}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3051 次 |
最近记录: |