Nicotine

Open source framework for PHP & MySQL

Downloads Page

Forms

Nicotine Framework has full support for generating HTML forms, without losing code flexibility. Below you have some concrete examples which you can put into view. You may want to adapt them as you need. Each PHP example is followed by the generated HTML code.

<?php
declare(strict_types=1);
namespace 
nicotine;
use 
nicotine\Form;
$form = new Form();
$form->isXHTML false// optional, default is true.
?>
<?php print $form->open()->method('post')->action(href('admin/users/add'))->autocomplete('off')->id('form-lorem'); ?> <form method="post" action="http://nicotine-framework.local/admin/users/add.html" autocomplete="off" id="form-lorem" enctype="multipart/form-data">
<?php
// Set config option "checkCsrfToken" to false, if you don't want forms to use this feature.
print $form->csrf();
?>
<input type="hidden" name="csrf" value="a541c329400d3e4373a967f4eaa7e4fd" />
<?php print $form->label()->for('user-name')->text(__('Choose an username')); ?> <label for="user-name">Choose an username</label>
<?php print $form->input()->type('text')->name('username')->value(transient('username''John Default'))->id('user-name')->placeholder(__('Enter username')); ?> <input type="text" name="username" value="John Default" id="user-name" placeholder="Enter username" />
<?php print $form->input()->type('password')->name('password')->class('pwd pwd-default')->style('width:100%;')->required(true)->minlength(6); ?> <input type="password" name="password" class="pwd pwd-default" style="width:100%;" required="required" minlength="6" />
<?php print $form->textarea()->name('description')->text('Some long text inner...')->placeholder(__('Enter description'))->custom('some-attr="some-value"'); ?> <textarea name="description" placeholder="Enter description" some-attr="some-value">Some long text inner...</textarea>
<?php
$checked 
true;
print 
$form->input()->type('checkbox')->name('ages[]')->value(3)->data(['is-foo' => 'foo''is-bar' => 'baz'])->checked($checked)->disabled(true);
?>
<input type="checkbox" name="ages[]" value="3" data-is-foo="foo" data-is-bar="baz" checked="checked" disabled="disabled" />
<?php print $form->input()->type('radio')->checked($checked)->value(4)->name('is_lorem')->class('some-class')->style('float:left;'); ?> <input type="radio" checked="checked" value="4" name="is_lorem" class="some-class" style="float:left;" />
<?php
$isDisabled 
true;

$flat = [
    
'23' => 'Twenty three',
    
'24' => 'Twenty four',
];

print 
$form->select()
    ->
content('flat')
    ->
name('choose_location[]')
    ->
disabled($isDisabled)
    ->
options($flat)
    ->
disabledOptions([23])
    ->
selectedOptions([24])
    ->
multiple(true)
    
    
// You may want to omit these methods, when multiple(true).
    
->emptyValue('')
    ->
emptyText(__('Select some locations'))
;
?>
<select name="choose_location[]" disabled="disabled" multiple="multiple">
    <option value="">Select some locations</option>
    <option value="23" disabled="disabled">Twenty three</option>
    <option value="24" selected="selected">Twenty four</option>
</select>

<?php
$groupFlat 
= [
    
'Small' => [
        
"2" => 'Two',
        
"3" => 'Three',
    ],
    
'Large' => [
        
"90" => 'Ninety',
        
"100" => 'One hundred',
    ],
];

print 
$form->select()
    ->
content('groupFlat')
    ->
name('choose_location[]')
    ->
disabled($isDisabled)
    ->
options($groupFlat)
    ->
disabledOptions([290])
    ->
selectedOptions([100])
    ->
multiple(true)
    
    
// You may want to omit these methods, when multiple(true).
    
->emptyValue('')
    ->
emptyText(__('Select some locations'))
;
?>
<select name="choose_location[]" disabled="disabled" multiple="multiple">
    <option value="">Select some locations</option>
    <optgroup label="Small">
        <option value="2" disabled="disabled">Two</option>
        <option value="3">Three</option>
    </optgroup>
    <optgroup label="Large">
        <option value="90" disabled="disabled">Ninety</option>
        <option value="100" selected="selected">One hundred</option>
    </optgroup>
</select>

<?php
$multi 
= [
    [
'id' => 23'name' => 'Twenty three'],
    [
'id' => 24'name' => 'Twenty four']
];

print 
$form->select()
    ->
content('multi')
    ->
name('choose_location[]')
    ->
columns('id''name'// key, value
    
->disabled($isDisabled)
    ->
options($multi)
    ->
disabledOptions([23])
    ->
selectedOptions([24])
    ->
multiple(true)
    
    
// You may want to omit these methods, when multiple(true).
    
->emptyValue('')
    ->
emptyText(__('Select some locations'))
;
?>
<select name="choose_location[]" disabled="disabled" multiple="multiple">
    <option value="">Select some locations</option>
    <option value="23" disabled="disabled">Twenty three</option>
    <option value="24" selected="selected">Twenty four</option>
</select>

<?php
$groupMulti 
= [
    
'Small' => [
        [
'id' => 2'name' => 'Two'],
        [
'id' => 3'name' => 'Three'],
    ],
    
'Large' => [
        [
'id' => 90'name' => 'Ninety'],
        [
'id' => 100'name' => 'One hundred'],
    ]
];

print 
$form->select()
    ->
content('groupMulti')
    ->
name('choose_location[]')
    ->
columns('id''name'// key, value
    
->disabled($isDisabled)
    ->
options($groupMulti)
    ->
disabledOptions([3])
    ->
selectedOptions([2100])
    ->
multiple(true)
    
    
// You may want to omit these methods, when multiple(true).
    
->emptyValue('')
    ->
emptyText(__('Select some locations'))
;
?>
<select name="choose_location[]" disabled="disabled" multiple="multiple">
    <option value="">Select some locations</option>
    <optgroup label="Small">
        <option value="2" selected="selected">Two</option>
        <option value="3" disabled="disabled">Three</option>
    </optgroup>
    <optgroup label="Large">
        <option value="90">Ninety</option>
        <option value="100" selected="selected">One hundred</option>
    </optgroup>
</select>

<?php print $form->button()->type('button')->id('trigger-lorem')->text(__('Click me'))->disabled(true); ?> <button type="button" id="trigger-lorem" disabled="disabled">Click me</button>
<?php print $form->input()->type('submit')->name('send_form')->value(__('Send')); ?> <input type="submit" name="send_form" value="Send" />
<?php print $form->close(); ?> </form>