Download the PHP package flsouto/htcheckb without Composer
On this page you can find all versions of the php package flsouto/htcheckb. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package htcheckb
HtCheckb
This library is used to create checkboxes.
Installation
Use composer:
Usage
The following example instantiates the checkbox class and renders it:
Output:
The extra hidden field is a submit flag which indicates if the checkbox has been sent (i.e. the form has been submited). This is important in the case you have a checkbox that is checked by default but is unchecked by the user. In case that a validation error occurs, the form must be shown again but with the checkbox unchecked (the default is ignored).
The next example renders the checkbox in readonly mode (chekcbox button disabled):
Output:
Notice that a second hidden field is rendered as well. This is because when you disable a form field it will not be sent by browsers, and it is important to mantain the state of the data on a submit event even if the field is on readonly mode.
Changing the defaults
By default, the checkbox understands "1" as true and "0" as false. It also is unchecked (state "0") by default. The below example changes all of that, so that 'true' means 'checked' and 'false' means unchecked. It also makes the field checked by default:
Outputs:
In the following example we are going to simulate a situation where the form is submited (notice the presence of 'newsletter_submit' flag) but the checkbox key (i.e. 'newsletter') is not present. This means the user would have unchecked the checkbox that was checked by default. So in this case the checkbox is rendered without the 'checked' attribute:
Outputs:
Last but not least, I want to show you that the checkbox can also represent the state of a supposed database row which uses the common Y/N pattern to indicate if it is active or not. Notice that the checkbox is rendered unchecked even though it is told to be checked by default:
Output: