2017 © Pedro Peláez
 

library htbutton

Class for generating form buttons

image

flsouto/htbutton

Class for generating form buttons

  • Monday, January 23, 2017
  • by flsouto
  • Repository
  • 0 Watchers
  • 0 Stars
  • 20 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

HtButton

Overview

This class allows you to easily create html form buttons in an object oriented way and comes with the following features:, (*1)

  • Setting attributes and styles with ease
  • Shortcut for inline/block layout
  • Checking for form submission

Notice: this class extends an abstract class called HtField. The inherited functionality will not be covered here. So, if you find difficulty in understanding something, please refer to this documentation., (*2)

Installation

Use composer:, (*3)

composer require flsouto/htbutton

Usage

The below code simply creates and renders a button named "action". Notice that by default the button's label is its own name with the first letter in upper case:, (*4)

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

$button = new HtButton("action");

echo $button;

The above code will produce the following output:, (*5)

<button id="58854a02812ac" name="action" style="display:block" value="58854a02812ac">Action</button>

Specifing a label

The second parameter to the constructor accepts a label string:, (*6)

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

$button = new HtButton("action", "Save");

echo $button;

The output will be:, (*7)

<button id="58854a028153c" name="action" style="display:block" value="58854a028153c">Save</button>

Using the label setter

If you desire to set label after construction, use the label setter:, (*8)

use FlSouto\HtButton;

$button = new HtButton("action");
$button->label("Save");

echo $button;

Outputs:, (*9)

<button id="58854a02816d6" name="action" style="display:block" value="58854a02816d6">Save</button>

Inline/Block Layout

By default the button is rendered in block mode, meaning that it will always appear in a new line. In order to change it so that it is rendered in the same line, you have to call the inline method:, (*10)


$button = new HtButton("action"); $button->inline(true); echo $button;

The output will be:, (*11)

<button id="58854a028188d" name="action" style="display:inline-block;vertical-align:text-top" value="58854a028188d">Action</button>

As you can see, it has simply added some css properties to the button's "style" attribute., (*12)

Calling the same method with a false argument will return to the block layout:, (*13)


$button = new HtButton("action"); $button->inline(true); $button->inline(false); echo $button;

Output:, (*14)

<button id="58854a0281a2a" name="action" style="display:block;vertical-align:text-top" value="58854a0281a2a">Action</button>

Adding custom CSS

Pass an array to the àttrs method with a style key like in the example below:, (*15)


$button = new HtButton("action", "Delete"); $button->attrs(['style'=>['background'=>'red','color'=>'yellow']]); echo $button;

Outputs:, (*16)

<button id="58854a0281bb7" name="action" style="display:block;background:red;color:yellow" value="58854a0281bb7">Delete</button>

Changing default value

The value that gets sent on a form submission event is by default a random string which is the same as the button's id. So, if you want to change that, you have to change the id attribute:, (*17)


$button = new HtButton("action", "Delete"); $button->attrs(['id'=>'delete']); echo $button;
<button id="delete" name="action" style="display:block" value="delete">Delete</button>

Checking if button has been pressed

If you nested your button inside a form, chances are you will need at some point to check if that button has been pressed so you can process the requested action:, (*18)


$_REQUEST['delete'] = 1; // pretend a form has been submitted $button = new HtButton('delete'); $button->context($_REQUEST); if($button->value()){ echo 'Deleting...'; }

Outputs:, (*19)

Deleting...

Handling multiple buttons

Sometimes you may want to have more than one action available in a form. In this case you will probably need to have muliple buttons and be able to tell which one has been clicked. The following snippet shows you how to do just that:, (*20)


$_REQUEST['action_delete'] = 1; // pretend a form has been submitted $updateBtn = (new HtButton('action_update'))->context($_REQUEST); $deleteBtn = (new HtButton('action_delete'))->context($_REQUEST); if($updateBtn->value()){ echo 'Updating...'; } if($deleteBtn->value()) { echo 'Deleting...'; }

The output will be:, (*21)

Deleting...

Handling buttons with same name

I wouldn't recommend giving the same name to different buttons on the same form, but, since I've seen this pattern been used by many developers I decided to include an example of how one could handle that problem through this API. You basically have to give a different id/value to each button and check which one has been sent in a switch block:, (*22)


$_REQUEST['action'] = 'update'; $updateBtn = new HtButton('action','Update'); $updateBtn->attrs(['id'=>'update'])->context($_REQUEST); $deleteBtn = new HtButton('action','Delete'); $deleteBtn->attrs(['id'=>'delete'])->context($_REQUEST); if(isset($_REQUEST['action'])){ switch($_REQUEST['action']){ case $updateBtn->id() : echo 'Updating...'; break; case $deleteBtn->id() : echo 'Deleting...'; break; } }

The output will be:, (*23)

Updating...

The Versions

23/01 2017

dev-master

9999999-dev

Class for generating form buttons

  Sources   Download

The Requires

 

by Fabio Souto

html forms fields

23/01 2017

1.0.0

1.0.0.0

Class for generating form buttons

  Sources   Download

The Requires

 

by Fabio Souto

html forms fields