This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

pwtan14262
pwtan14262

Hi,

In model/controller, how do we know if the checkbox is checked? I would to do something like

if(Checked){ //do something }

Thanks in advance

Daniel81
Daniel81
if ($this->checkbox_field_name) {
       // do something
}
pwtan14262
pwtan14262

Hi Daniel81,

Thanks for your answer! But this code only works if i save the model. Is there a way to do it without saving the model? Thanks

Daniel81
Daniel81

Without saving the model there's null value, so not sure how you'd check for a checkbox checked that doesn't technically exist until the model is saved

Last updated

pwtan14262
pwtan14262

Hi Daniel81,

Thanks for your reply. Is there no other way to get the current checkbox status? Also, how can i manually save the model? I thinking along the line of manually saving a model by press a button widget and get the checkbox status. Thanks

Crazymodder
Crazymodder

This check can only be done on client side. So use javascript or jQuery for that:

if($("#idOfYourCheckbox").is(':checked')) {
    alert('checked')
}  else {
     alert('not checked');
}

Instead of alert you can surely make a ajax post to backend and you can respond in your controller to that.

Last updated

LilEssam
LilEssam

Well, It's very easy.

I'll make an example for you.

Within your controller you decided to make a button that do something when the user select one record or more.

Here's the button

<button
        class="btn btn-default oc-icon-trash-o"
        disabled="disabled"
        onclick="$(this).data('request-data', {
        checked: $('.control-list').listWidget('getChecked')
        })"
        data-request="onDelete"
        data-request-confirm="<?= e(trans('lilessam.maintenancesystem::lang.sure')) ?>"
        data-trigger-action="enable"
        data-trigger=".control-list input[type=checkbox]"
        data-trigger-condition="checked"
        data-request-success="$(this).prop('disabled', true)"
        data-stripe-load-indicator>
        <?= e(trans('backend::lang.list.delete_selected')) ?>
        </button>

Notice that I'm using Ajax, So in my controller I defined a function called onDelete Here's how I know if the checked record.

 /** Delete items from the list. Ajax call **/
    public function onDelete() {
        /** Check if this is even set **/
        if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) {

          /** Check if there's more than one record **/
          if(count($checkedIds) > 1) :
          /** There's more than one record checked **/
          /** cycle through each id **/
            foreach ($checkedIds as $objectId) {
                /** Check if there's an object actually related to this id
                  **/
                if (!$object = \Lilessam\Maintenancesystem\Models\Equipment::find($objectId))
                    continue;  /** Screw this, next! **/
                //Do delete the record
                $object->delete();
            }
          else:
          /** There's only one record checked **/
            \Flash::error('You have to select more than one record');
          endif;

        }
        /** Return the new contents of the list, so the user will feel as if
          * they actually happened something
          **/
        return $this->listRefresh();
    }
pwtan14262
pwtan14262

Hi LilEssam,

Thanks for your reply, but it the code where did $checkedIds come from? Thanks

LilEssam
LilEssam

As you can see we put post('checked') in $checkedIds

So the javascript code which I showed you sends a post request with the name checked to onDelete function in controller.

You can see it in

 onclick="$(this).data('request-data', {
        checked: $('.control-list').listWidget('getChecked')
        })"

Last updated

1-9 of 9

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.