Form class can be used,
a form model must be initialized. Models contain rules which the form can use in order to
validate created forms. These rules are then applied on each form based
on their relativity with the database. The Form class
accepts static calls on all form input types except a few like date-local and password
fields.
model methodinit or open method
<?php
namespace spoova\mi\windows\Model;
use spoova\mi\core\classes\Model;
class FormModel extends Model{
public function rules() : array {
return []; // Form Validation rules
}
}
rules()
method which usually contain form validation rules. Once the model is created,
the instance of that model will be loaded into the Form class for management.
This is shown below.
Form::model(new FormModel);
$Form : referenced variable anchoring Form class instance (optional)
method : request method (optional)
action : form action (optional)
type : form input type (e.g email, password...)
init() method is used to open a form class in a automatic display mode.
This means that when it is used, the form generated will be automatically printed out to the
page.
Form::init($Form, method, action);
$Form will anchor the form instance itself.
//starting a new form in rendering mode
Form::init($Form, 'post', 'somepage.php');
open() method is similar to the init() method
except that rather than printing directly, it returns the generated content of a form. It takes the same parameters as the init() method.
//starting a new form without automatic display
echo Form::open($Form, 'post', 'somepage.php');
set() method is used to overide the default form settings.
Form::set($array);
where:
$array = list of options which can be contains array index:
form_class => This sets the forms global class attribute value which is applied on all Form::Group()
field_class => This sets the form inputs class attribute value which is applied on all Form::Field()
Form::set([
'form_class' => 'form-flex', // add form-flex class to all form groups
'field_class' => 'field-item bg-primary' // add bg-primary class to all form fields
]);
Form::Field($type, $name, $attrs); // supplies data to be hashed. where: $type : type of input field (e.g password) $name : the name attribute value of the input field $attrs : other added attributes and value pairs
// add a new form input password field Form::Field('password', 'passfield'); // add a new form input email field with attributes Form::Field('email', 'email', ['addClass'=>'i-flex']); // add a new form input field by calling the input type Form::Email('email', ['addClass'=>'i-flex']); Supported types: Email, Text, TextBox/Textarea, Pass/Password, Range, Radio, Checkbox, Hidden, File, Number, Tel, Url, Date, DateLocal {date-local}, Week, Month, Year, Image, Color, CheckBox, Radio, Search, Submit, Button
Form::label($attrs, $content);
where:
$attrs: supplied attributes
$content : text content of the label
Form::label(['class'=>'label'], 'Username'); // <label class="label">Username</label>
Form::Group(tag, content);
where:
tag: html tag name (e.g div)
content: function or string
Form::Group('div', fn() =>
Form::Text('firstname').
Form::Text('lastname')
);
Form::Group('div class="i-flex"', fn() =>
Form::Group('div', fn() =>
Form::Text('firstname').
Form::Text('lastname')
)
);
Form::GroupEach($tagname, $content); // group each where: $tagname : html wrapper tag (e.g div) $content : function or string
Form::GroupEach(
'div class="field"',
Form::Text('firstname').
Form::Pass('lastname')
);
Form::open() or Form::Init(),
It is expected to be closed using Form::close() which closes the form tag.
Form::close(); //close a tag
Form::GroupEach(
'div class="field"',
fn() =>
Form::Text('firstname').
Form::Pass('lastname').
Form::close()
);
Form::model($model); // check here for how to create a model
Form::init($form, 'get');
//displays automatically because init() was used
Form::Group('div', fn() =>
Form::GroupEach('div class="inputs"', fn() =>
Form::Field('email', 'email', ['placeholder' => 'email'])
.Form::Pass('password', ['placeholder' => 'password'])
.Form::close()
)
);
Form::model($model); // check here for how to create a model
Form::open($form, 'post');
//using echo to display result
echo Form::Group('div', fn() =>
Form::GroupEach('div class="inputs"', fn() =>
Form::Field('email', 'email', ['placeholder' => 'email'])
.Form::Pass('password', ['placeholder' => 'password'])
.Form::close()
)
);