checkboxes on submit not showing correct values. Shows 0 always
While we can't see how your form is built completely, it might be that your custom attributes are affecting the element "name" in the form state.

i.e change

'#attributes' =&gt; array('name' =&gt; 'field-'.$code.'-'.$id),


to 

'#attributes' =&gt; array('data-name' =&gt; 'field-'.$code.'-'.$id),