Reputation: 346
I have a checkbox and i want to make my checkbox checked by default, if the user unchecks the checkbox and save the form, i need the checkbox to be unchecked while retrieving or editing the form data.
My view page
<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>
Giving this brings back the old data from database correctly but i couldn't make my checkbox checked as default
Controller:
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'));
}
I tried
{{ Form::checkbox('active', 1, true, Input::old('active', $assetdetail->active), array('class'=>'isnesdbox')) }}
But i get the follwing error
Cannot use a scalar value as an array
I also tried this
<input class="col-md-1 controls isnesdbox" type="checkbox" name="active" checked id="active" value="1" {{ $assetdetail->active === '1' ? 'checked' : '' }} />
Please help me to achieve this Note:iam using mysql database and the database type iam using is bit which stores 0's and 1's depending on the user input.
Upvotes: 5
Views: 6439
Reputation: 8796
In below, if $myDefault
is set to true
, it equals checking the check-box by default.
<input type="checkbox" value="myOnValue"
name="myField"
@if((!old() && $myDefault) || old('myField') == 'myOnValue') checked="checked" @endif />
<input type="checkbox" value="on"
name="myField"
@if(!old() || old('myField') == 'on') checked="checked" @endif />
Below does not need "!old()
" workaround.
<input type="checkbox" value="on"
name="myField"
@if(old('myField') == 'on') checked="checked" @endif />
Related Laracast: https://laracasts.com/discuss/channels/laravel/how-to-set-old-value-for-checkbox-input-using-blade?page=1&replyId=771230
Upvotes: 0
Reputation: 126
Since Google apparently still thinks this answer is relevant, here's an updated answer for Laravel 5.8:
<input type="checkbox" value="true" name="notify" @if(!old() || old('notify') == 'true') checked="checked" @endif />
Upvotes: 11
Reputation: 62338
It sounds like you don't really care what the value of $assetdetail->active is, you just want the checkbox checked until the user unchecks it.
Does this do what you're looking for:
{{ Form::checkbox('active', 1, Session::hasOldInput() ? array_key_exists('active', Input::old()) : true, array('class'=>'isnesdbox')) }}
Or have I misread what you're trying to do?
For this solution, it checks if there is old input in the session. If there is old input data, it uses the existence of the 'active'
key to determine the state of the checkbox [1]. If there is not old input data, it defaults the checkbox to checked.
[1] Checkboxes submit differently then other form elements. An unchecked checkbox is not considered a "successful" control, so it does not submit any data, and will therefore not exist in the Input::old()
array. So, if the key exists, it was checked; if the key does not exist, it was not checked. You can read more about "successful" form controls here.
Based on the comments, it looks like the same form is used for both creating and editing the Assetdetail object. When creating, you want the checkbox checked by default, when editing you want the checkbox to reflect the value stored in the database.
The code I posted above should be close, but you'll need to change the hardcoded true
in the ternary operator.
If $assetdetail is null during create, you'll want:
{{ Form::checkbox('active', 1, Session::hasOldInput() ? array_key_exists('active', Input::old()) : (!empty($assetdetail) ? $assetdetail->active : true), array('class'=>'isnesdbox')) }}
If $assetdetail is an Assetdetail instance during create, you'll want:
{{ Form::checkbox('active', 1, Session::hasOldInput() ? array_key_exists('active', Input::old()) : ($assetdetail->exists ? $assetdetail->active : true), array('class'=>'isnesdbox')) }}
Upvotes: 5