dev-master
9999999-devClass for generating form buttons
The Requires
by Fabio Souto
html forms fields
1.0.0
1.0.0.0Class for generating form buttons
The Requires
by Fabio Souto
html forms fields
Wallogit.com
2017 © Pedro Peláez
Class for generating form buttons
This class allows you to easily create html form buttons in an object oriented way and comes with the following features:, (*1)
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)
Use composer:, (*3)
composer require flsouto/htbutton
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>
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>
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>
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>
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>
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>
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...
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...
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...
Class for generating form buttons
html forms fields
Class for generating form buttons
html forms fields