2017 © Pedro Peláez
 

library htcheckb

Creates checkbox field

image

flsouto/htcheckb

Creates checkbox field

  • Thursday, February 16, 2017
  • by flsouto
  • Repository
  • 0 Watchers
  • 0 Stars
  • 17 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

HtCheckb

This library is used to create checkboxes., (*1)

Installation

Use composer:, (*2)

composer require flsouto/htcheckb

Usage

The following example instantiates the checkbox class and renders it:, (*3)

<?php
use FlSouto\HtCheckb;
require_once('vendor/autoload.php');

$checkbox = new HtCheckb("newsletter","Receive newsletter?");

echo $checkbox;

Output:, (*4)




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)., (*5)

The next example renders the checkbox in readonly mode (chekcbox button disabled):, (*6)

use FlSouto\HtCheckb;
require_once('vendor/autoload.php');

$checkbox = new HtCheckb("newsletter");
$checkbox->readonly();

echo $checkbox;

Output:, (*7)




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., (*8)

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:, (*9)

use FlSouto\HtCheckb;

$checkbox = new HtCheckb('newsletter','Newsletter','true','false');
$checkbox->fallback('true');

echo $checkbox;

Outputs:, (*10)




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:, (*11)


$checkbox = new HtCheckb('newsletter','Newsletter','true','false'); $checkbox->fallback('true')->context(['newsletter_submit'=>1]); echo $checkbox;

Outputs:, (*12)




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:, (*13)


// let's pretend this data was loaded from the database $row = ['active'=>'N']; $checkbox = new HtCheckb('active','Active','Y','N'); $checkbox->fallback('Y'); // check by default $checkbox->context($row); // set the form's state echo $checkbox;

Output:, (*14)




The Versions

16/02 2017

dev-master

9999999-dev

Creates checkbox field

  Sources   Download

The Requires

 

by Fabio Souto

html forms widgets checkbox fields

16/02 2017

1.0.0

1.0.0.0

Creates checkbox field

  Sources   Download

The Requires

 

by Fabio Souto

html forms widgets checkbox fields