PHP code example of tuum / form

1. Go to this page and download the library: Download tuum/form library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tuum / form example snippets


$view = new Tuum\Form\DataView();
$view->setData([
    'name' => 'my-name',
    'bold' => '<b>bold</b>',
    'list' => ['v1', 'v2'],
]);
$view->setInputs([
    'name' => 'old-name',
    'more' => [
        'key' => 'val'
    ]
]);
// getting escaped data. 
echo $view->data['name']; // 'my-name'
echo $view->data['bold']; // escaped <b>bold</b>
echo $view->data->get('bold'); //  escaped <b>bold</b>
echo $view->data->raw('bold'); //  raw <b>bold</b>
// and arrays.
$list = $view->data->extractKey('list');
echo $list[0]; // 'v1'
echo $list[1]; // 'v2'

// getting old inputs
echo $view->inputs->get('name', $data['name']); // 'old-name'
echo $view->inputs->get('more[key]'); // val
// and use it in form generator. 
echo $view->forms->text('name', 'default'); 
// will output <input type="text" value="old-name">

$esc = new Escape();
echo $esc('<danger>safe</danger>'); // or
echo $esc->escape('<danger>safe</danger>');

$esc = new Escape('addslashes');
// or
$esc = $esc->setEscape('rawurlencode');

$view = new DataView(new Escape('addslashes'));
$view->inputs->get('with-slash'); // escaped with addslashes.
// or change how to escape
$view->escape->setEscape('rawurlencode');

// construct yourself. 
$data = Data::forge(['view'=>'<i>val</i>'], $escape);

// or use DataView class. 
$view = new DataView();
$view->setData(['some'=>'value']);
$data = $view->data;

echo $data['view'];      // escaped
echo $data->view;        // escaped
echo $data->get('view'); // escaped
echo $data->raw('view'); // raw value
echo $data->get('none', 'non\'s'); // show escaped default value

$data1 = [
    'text' => 'testing',
    'more' => '<b>todo</b>',
];
$data2 = [
    'text' => 'tested',
    'more' => '<i>done</i>',
];
$data = Data::forge([$data1, $data2]);
foreach($data as $key => $val) {
    echo "$key: ", $val->text;
    echo "$key: ", $val->more; // escaped. 
}

> $data = Data::forge([
>     'text' => 'tested',
>     'more' => '<i>done</i>',
> ]);
> foreach($data as $key => $val) {
>     echo "$key: ", $val->get(null); // won't work!
> }
> 

$data = Data::forge([
    'obj'=>new ArrayObject['type' => 'object']
]);
$obj = $data->extractKey('obj');
echo $obj->type;  // object

$data = Data::forge(['_method'=>'put']);
echo $data->hiddenTag('_method');  
// <input type="hidden" name="_method" value="put" />


$input = Inputs::forge([
    'name' => '<my> name',
    'gender' => 'male',
    'types' => [ 'a', 'c' ],
    'sns' => [
        'twitter' => '[email protected]',
        'facebook' => '[email protected]',
    ],
], $esc);

echo $input->get('name'); // escaped '<my> name'
echo $input->get('sns[twitter]'); // '[email protected]'
vardump($input->get('types')); // ['a', 'c']

// supply name of elements and its value. 
echo $input->selected('gender', 'male');   // 'selected'
echo $input->selected('gender', 'female'); // empty
echo $input->checked('types', 'a'); // ' checked'
echo $input->checked('types', 'b'); // empty


$errors = Errors::forge([
    'name' => 'message for name',
    'gender' => 'gender message',
    'types' => [ 2 => 'message for type:B' ],
    'sns' => [
        'facebook' => 'love messaging facebook?',
    ],
], $esc);
// default format is: <p class="text-danger">%s</p>
echo $errors->p('name'); // message for name
echo $errors->p('gender');   // gender message
echo $errors->p('types[2]'); // message for type:B
echo $errors->p('sns[facebook]'); // love messaging facebook?

$errors->format = '<div>(*_*) %s</div>';

$message = Message::forge();
$message->add('hello');
$message->add('whoops', Message::ERROR);

echo $message;
// shows two divs for hello and whoops. 

echo $message->onlyOne(); 
// <div class="alert alert-danger">Whoops</div>

$message->formats = [
    self::MESSAGE => '<div>:) %s</div>',
    self::ALERT   => '<div>:| %s</div>',
    self::ERROR   => '<div>:( %s</div>',
];

$form = new Form();

// or using DataView
$view = new DataView();
$form = $view->forms;

$forms = $forms->withInputs($inputs);

$forms->text('safe', $data->get('safe'));

<?= $form->input('text', 'name', 'default value'); 

<?= $form->date('radio')->class('form')->placeholder('date')->checked; 

$form->$method(string $name, string $value = null);

<?= $form->open()->action('to')->method('post')->uploader(); 

<?= $form->open()->action('to')->method('put', 'method_token'); 

$form->label('label string', 'for-id');

$form->submit('button name');
$form->reset('cancel me');

$form->textArea('area-name', 'default value');

$list = [
    '1' => 'selecting',
    '2' => 'made',
    '3' => 'easy',
];
echo $form->select('name', $list, '2');

$list = [
    '1' => 'checkbox',
    '2' => 'radio',
];
echo $form->checkList('checks', $list, '1');

$list = $form->checkList('radio', $list, '2');
foreach($list as $key => $html) {
   echo $list->getLabel($key), ': ', $html, '<br/>';
}

$form = new Dates();
echo $dates->setYear(
    Lists::years(2014, 2016)
)->dateYMD('my-date', '2015-06-18');

    
echo $date->setYear(
    Lists::years(2014, 2016)->useJpnGenGou()
)->setMonth(
    Lists::months()->useFullText()
)->withClass('tested-class')
->dateYM('test')
->resetWidth('123px');