OctoberCMS Builder Relation Checkboxes Default Value

326 Views Asked by At

I have a many-to-many relationship which is represented by checkbox in the back-end forms. How could I set the form up to have all the checkbox checked when creating a new record?

1

There are 1 best solutions below

0
On

hmm not sure we can do it in correct way because actually to make that check box selected we need actual relation in DB but at that moment we don't have that so, but we can make some hack for it

ok, so first if you created form using form builder, you have controller and its view files

now go to its view files find create.htm now find this line <?php Block::endPut() ?> and paste this code after. like this

<?php Block::endPut() ?>

/* just replace User[groups][] with your field name */
/* here User => your model name first latter caps */
/* [groups] => your relation name all small */
/* or you can inspect it and paste it here using devtools */
<script>
    jQuery(document).ready(function(){
        jQuery("[name='User[groups][]']").attr('checked', true)
        /* replace here ^ */
    })
</script>

these peace of code will check all the box when page loads.

its not the best way, kind of hack, as I didn't find any good easy alternative so.

please comment if you find any difficulties.