2017 © Pedro Peláez
 

library htwidget

Abstract class for defining form widget types

image

flsouto/htwidget

Abstract class for defining form widget types

  • Friday, March 31, 2017
  • by flsouto
  • Repository
  • 0 Watchers
  • 1 Stars
  • 69 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

HtWidget

Overview

This class can be used to define different types of widgets. But what is a widget? A widget is an interactive field/element which has some sort of state and allows users to communicate with a server or a backend. Notice that not all form fields are interactive. A hidden field, for instance, even though it has a state, it does not provide any form of direct interaction. A button is also not a widget, in my opinion, because it doesn't allow any complex interaction other than just clicking on it. An input text field or a checkbox, on the other hand, can be considered to be a widget because they do have a state and require some kind of interaction. In this documentation we are going to see how to implement a simple TextField class which inherits from HtWidget., (*1)

Notice: A lot of functionality is inherited from a more basic abstract class called HtField. If you find difficulties in understanding some of the features being reused here, please refer to this documentation., (*2)

Installation

Run composer:, (*3)

composer require flsouto/htwidget

Usage

Below we are going to implement a simple TextField class. Pay attention to it because we are going to use it through out this document in order to learn about all the functionality inherited from HtWidget:, (*4)

<?php

class TextField extends HtWidget{

    function __construct($name){
        parent::__construct($name);
        // By default all HtWidget instances have a default, random id.
        // We want to change that so the id is always the name of the field itself.
        $this->attrs['id'] = $name;
    }

    // All concrete HtWidget implementations must define a renderWritable method
    function renderWritable(){
        $attrs = $this->attrs;
        $attrs['value'] = $this->value();
        echo '<input '.$attrs.' />';
    }

    // All concrete HtWidget implementations must define a renderReadonly method
    function renderReadonly(){
        $attrs = $this->attrs;
        echo "<span ".$attrs.">".$this->value()."</span>";
    }

}

In the next example we instantiate the newly defined class and render it., (*5)

<?php
require_once('vendor/autoload.php');

$field = new TextField('email');
$field->context(['email'=>'someuser@domain.com']);

echo $field;

Notice that by default the widget is rendered in "writable mode":, (*6)




Switch to readonly mode

To render the readonly version of your widget, simply call the readonly setter with a positive argument:, (*7)

require_once('vendor/autoload.php');

$field = new TextField('email');
$field->context(['email'=>'someuser@domain.com'])
    ->readonly(true);

echo $field;

Output:, (*8)




Understanding how it works

All those extra tags surrounding the main element are produced by default. When we echo $field the HtWidget::render() method gets called which produces a wrapper containing the output of HtWidget::renderInner(), which in turn decides which mode we are in and calls the respective renderReadonly or renderWritable method, along with error messages. So, if you wanted to display only the inner content, without the wrapper, you would have to call the renderInner() method:, (*9)

$field->renderInner()

Show the widget inline

By default the widget is rendered in block mode, which means it occupies the entire line. If you want it to appear in the same line as whatever was printed before, use the inline setter:, (*10)


$field = new TextField('username'); $field->inline(true); echo $field;

Output:, (*11)




Labels

By default the widget is rendered without an associated label. You have to specify one if you want it to be displayed:, (*12)


$field = new TextField('product_name'); $field->label('Product Name'); echo $field;

Output:, (*13)




You can change the label's tag attributes by passing an associative array. In that case you have to use the special text attribute in order to set the label's text:, (*14)


$field = new TextField('product_name'); $field->label(['text'=>'Product Name', 'class'=>'some_class']); echo $field;

Output:, (*15)




By default, the label is rendered above the widget. If you want it to be displayed in the same line you can use the special inline attribute:, (*16)


$field = new TextField('name'); $field->label(['text'=>'Name','inline'=>true]); echo $field;

Output:, (*17)




Set the field as required

Call the required method passing the error message to be shown in case the field is left blank:, (*18)


$field = new TextField('name'); $field ->required('Name is required!') ->context(['name'=>'']); echo $field->validate();

Output:, (*19)

Name is required!

Activate error messsages

By default, error messages are not displayed along the field if any validation error occurs internally. You can change that by calling the error method with a positive value:, (*20)


$field = new TextField('name'); $field ->required('Name is required!') ->context(['name'=>'']) ->error(true); echo $field;

Output:, (*21)




Name is required!

You can customize the error message tag by passing an array of attributes to the error function., (*22)


$field = new TextField('name'); $field->required('Name is required!') ->context(['name'=>'']) ->error(['display'=>true,'class'=>'errmsg','style'=>['padding'=>'5px']]); echo $field;

Output:, (*23)




Name is required!

Notice that in this case we enable the error display by setting the 'display' attribute., (*24)

Disable error displaying by passing a negative (i.e. false) argument:, (*25)


$field = new TextField('name'); $field->required('Name is required!') ->context(['name'=>'']) ->error(['class'=>'errmsg','style'=>['padding'=>'5px']]) ->error(false); echo $field;

Output:, (*26)




Notice: you could instead pass the 'display' attribute set to false, (*27)

Make error tag display in the same line using inline option:, (*28)


$field = new TextField('name'); $field->required('Name is required!') ->context(['name'=>'']) ->error(['display'=>true,'inline'=>true]); echo $field;

Output:, (*29)




Name is required!

Specify a default value

You can setup a default value to be used in case the field is left blank and/or a validation error occurs:, (*30)


$field = new TextField('amount'); $field->fallback(1); echo $field;

Output:, (*31)




The Versions

31/03 2017

dev-master

9999999-dev

Abstract class for defining form widget types

  Sources   Download

The Requires

 

by Fabio Souto

html forms widgets fields

31/03 2017

1.0.2

1.0.2.0

Abstract class for defining form widget types

  Sources   Download

The Requires

 

by Fabio Souto

html forms widgets fields

31/03 2017

1.0.1

1.0.1.0

Abstract class for defining form widget types

  Sources   Download

The Requires

 

by Fabio Souto

html forms widgets fields

05/02 2017

1.0.0

1.0.0.0

Abstract class for defining form widget types

  Sources   Download

The Requires

 

by Fabio Souto

html forms widgets fields