Renaat is not quite correct in what he has written.
- dates has nothing to do with trying to see if a field can contain special characters, but a quite legitimate attempt to return an array of results from the form. The reason it doesn't work is that all the elements end up with the same index once the Moodle form processing has taken place - what you need to do is use the $id in the name - i.e. "dates[$id]"
- null / null: you should provide a label either before or after the checkbox, otherwise users have no way of knowing what they are ticking (but that isn't the reason the submission isn't working)
- first array: agreed, that is meant to be the attributes to apply, not the values
- second array: agreed, that is where you should define the value of the checkbox
So the solution would look something like this:
$mform->addElement('advcheckbox', 'dates', null, 'Opt 1', , [0, 2]);
In this case - I've used '6' and '2' as arbitrary values, you could also write:
$mform->addElement('advcheckbox', "dates[$id]", null, 'Opt 1', , [0, $id]);
(on submission, this would look like: [2 => 2, 3 => 0, 4 => 0, 5 => 5], assuming that 2 + 5 were checked)
or you could simply use:
$mform->addElement('advcheckbox', "dates[$id]", null, 'Opt 1');
(on submission, this would look like: [2 => 1, 3 => 0, 4 => 0, 5 => 1], assuming 2 + 5 were checked)
If you want the unchecked values to be excluded from the submission completely, then use 'checkbox' instead of 'advcheckbox' (which uses hidden fields to ensure that a value is always present for every item).