简体   繁体   中英

OctoberCMS Builder Relation Checkboxes Default Value

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM