Forms

This class is simply used to generate html forms. Before a 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.

Initializing Form class
To initialize a form element, the following proceedures must be followed
  • create a form model class with a method rules that returns an array
  • Initialize the Form class by supplying an instantiated form model into the Form model method
  • Open the Form class using either init or open method
  • Create an input field using the field name on the Form class.
Create Form model (sample)
  <?php 

    namespace spoova\mi\windows\Model;

    use spoova\mi\core\classes\Model;

    class FormModel extends Model{

        public function rules() : array {

            return []; // Form Validation rules

        }

    }
                                        
In the above, a form model was created with the 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.
Instantiate form model
  Form::model(new FormModel);
                                        

The following keywords should be noted:

keywords
    
  $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
The 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.

Syntax: init
  Form::init($Form, method, action);
 
The referenced variable $Form will anchor the form instance itself.
Example: Opening form (init)
   //starting a new form in rendering mode
   Form::init($Form, 'post', 'somepage.php');
                                    

open
The 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.

Example: Opening form (open)
  //starting a new form without automatic display
  echo Form::open($Form, 'post', 'somepage.php');
                                

set
The set() method is used to overide the default form settings.

Syntax: set
  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() 
    
                                        
Example: set
    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

    ]);
                                        

Field
This method is used to add a new form input field. The syntax and examples are shown below.

Syntax
  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
                                        
                                        
Example
  // 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
  
                                        

Label
This method add an html label tag to forms.

Syntax
  Form::label($attrs, $content); 
        
    where:
    
    $attrs: supplied attributes
    $content : text content of the label
        
                                        
Example
  Form::label(['class'=>'label'], 'Username');  // <label class="label">Username</label> 
                                        

Group
This method is used to group input fields. A group can only contain a direct group child or children. A grandchild group is not supported.

Syntax
  Form::Group(tag, content); 
  
    where: 

        tag: html tag name (e.g div)
        content: function or string
  
                                        
Example 1
  Form::Group('div', fn() => 

    Form::Text('firstname').
    Form::Text('lastname')

  ); 
                                        
Example 2 : Adding class attribute and child Group
  Form::Group('div class="i-flex"', fn() => 

    Form::Group('div', fn() => 
        Form::Text('firstname').
        Form::Text('lastname')    
    )

  ); 
                                        

GroupEach
This method is used to apply a tag on each input element supplied. GroupEach can only be applied once.

Syntax
  Form::GroupEach($tagname, $content);  // group each 
        
    where: 

    $tagname : html wrapper tag (e.g div)
    $content : function or string
        
                                        
Example
  Form::GroupEach( 
    'div class="field"',

    Form::Text('firstname').
    Form::Pass('lastname')
  );
                                        
In the above, each input field created will have a wrapper of "div" with a class of field.
close
When a form is opened using Form::open() or Form::Init(), It is expected to be closed using Form::close() which closes the form tag.

Syntax: close
  Form::close(); //close a tag 
                                        
Example: GroupEach
  Form::GroupEach( 
    'div class="field"',

        fn() =>

            Form::Text('firstname').
            Form::Pass('lastname').
            Form::close()
  );
                                        

SAMPLE STRUCTURE
The example below is a sample structure of how to create form fields.

Example 1: sample structure
  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()

     )

  );
                                      
Example 2: sample structure
    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()

        )

    );