Contents

TextBox

The humble text box is perhaps the most common control of all appearing at least once in nearly every single form.

The TextBox control class has one property in addition to the standard control:

setMaxLength($length)
Sets the maximum number of characters the user can enter.
<?php

namespace Rhubarb\Leaf\Controls\Common\Examples\Text\TextBox;

use Rhubarb\Leaf\Leaves\Leaf;

class TextBox extends Leaf
{
    /**
    * @var TextBoxModel
    */
    protected $model;
    
    protected function getViewClass()
    {
        return TextBoxView::class;
    }
    
    protected function createModel()
    {
        return new TextBoxModel();
    }
}
                                    
<?php

namespace Rhubarb\Leaf\Controls\Common\Examples\Text\TextBox;

use Rhubarb\Leaf\Leaves\LeafModel;

class TextBoxModel extends LeafModel
{
    public $forename;
    public $surname;
    public $houseNumber;
}
                                    
<?php

namespace Rhubarb\Leaf\Controls\Common\Examples\Text\TextBox;

use Rhubarb\Leaf\Controls\Common\Text\TextBox;
use Rhubarb\Leaf\Views\View;

class TextBoxView extends View
{
    /**
    * @var TextBoxModel
    */
    protected $model;

    protected function createSubLeaves()
    {
        $this->registerSubLeaf(
            $forename = new TextBox("forename"),
            $surname = new TextBox("surname"),
            $houseNumber = new TextBox("houseNumber")
        );

        $forename->setPlaceholderText("Forename");
        $surname->setPlaceholderText("Surname");
        $houseNumber->setMaxLength(5);
        $houseNumber->setPlaceholderText("House No. (limited to 5 characters)");
    }

    protected function printViewContent()
    {
        print $this->leaves["forename"]." ".$this->leaves["surname"]." ".$this->leaves["houseNumber"];
    }
}