zendframework/zf2 — pull request #1469 by akandels (2012-06-08 21:27:32+00:00)
Title: FormCheckbox Helper to complement FormMultiCheckbox for single checkbox fields

Why:

The current FormMultiCheckbox is hardcoded for multiple elements.

Creating just a single checkbox is possible by passing one value in the "options" attributes, but the form value is returned (via $form->getData()) as an array, and creating checked/unchecked values is not possible and not very clear to someone wanting just a single checkbox field.

This creates a new helper FormCheckbox exclusively for a single checkbox with a checked value and an unchecked value, one of which will always be returned in the post data using the same technique as ZF1 employed.

I opted to use the options attribute's first and second child for the checked/unchecked values. I specify default options of 1 and 0 as that translates to the html's value attribute nicely. I wrestled with creating properties called something like checkedValue and uncheckedValue, but ultimately went with options for consistency with the other elements.

Tests:

Uses options attribute to generate checked and unchecked values
Uses element value to determine checkbox status
No options attribute creates default checked and unchecked values
Options attribute does not have two values

Results:
OK (10 tests, 15 assertions)