PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, February 23, 2022

[FIXED] How to make a checkbox checked by default and also retrieve old value from database in laravel

 February 23, 2022     checkbox, laravel, laravel-4, php     No comments   

Issue

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.


Solution

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.


Edit

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')) }}


Answered By - patricus
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing