Ness PHP Forms
Required Namespace
  • Ness
    • Forms
      • Form
    • Forms
      • FormElements

You can include this library to your code file by typing the below code;

use Ness\Forms\Form

use Ness\Forms\FormElements


About Forms #top

Ness PHP form is used to get data from user and send the data to model or controller. You can simply create forms and attach a model class to that form to manage user input. Form class is generally used in view files to fill a model class with user inputs.


Define A New Form #top

Create a form


$register->DefineForm(Array $attr = null) : String
                        

$attr: Form attributes


Set Model #top

If you did not want to set model in form definition you can set it with this function.


$register->setModel(String $model = "") : void
                        

$model: Model class name


Set Element #top

Add input or other elements to your form


$register->setElement(String $element = "") : String
                        

$element: A form elemnt. You can use FormElements Class


Finish Form #top

All forms started with DefineForm must end with FinishForm


$register->FinishForm() : String
                        

$relatedModel: Model Class Name to manage input
$attr: Form attributes


Call a function #top

Call a function in a model after form posted.


$register->Call(String $functionName) : String
                        

$functionName: Name of function in model class to run after form posted.

Warning Ness PHP framework fills automatically model class variables from form. You need to set name attribute of form, with same name of your variable in model class attached.


Example #top

Let's say you need the username, email and password of your user for registration. You need to crate a UserModel and a register form for that.

view\register.php


use Ness\UI\Page;
use Ness\Forms\Form;
use Ness\Forms\FormElements;

$view = new Page();
$register = new Form();


$view->SetLayout("master.phtml");


$view->BeginContent();

echo $register->DefineForm(array("class"=>"form-group", "method"=>"POST"));
        $register->setModel("UserModel");

    echo $register->setElement(FormElements::Label("E-mail", "email"));
    echo $register->setElement(FormElements::TextBox("email", array("class"=>"form-control")));

    echo $register->setElement(FormElements::Label("Username", "username"));
    echo $register->setElement(FormElements::TextBox("username", array("class"=>"form-control")));

    echo $register->setElement(FormElements::Label("Password", "password"));
    echo $register->setElement(FormElements::Password("password", array("class"=>"form-control")));


echo $register->setElement(FormElements::Submit("UserModel", array("class"=>"btn btn-success")));
    $register->Call("SaveUser");



$view->EndContent(); ?>
        
        
model\UserModel.php

                            
use Ness\Autopulse\Autopulse;
use Ness\Autopulse\MySqlConnect;

    
class UserModel
{
    public $username;
    public $email;
    public $password;
    
    
    
    public function SaveUser()
    {
    $con = new mysqlconnect("localhost","root","","papp");
    $myUser = array("username" => $this->username,
                    "email"=> $this->email,
                    "password"=> $this->password);
    
    if(Autopulse::LoadArray("users",$myUser,$con->Source()))
    {
        echo "< h1> User Registration Completed ";
    }else{ 
            echo "< h1> User Registration ERROR ";
    }
}
        
                                   
                            
Result:


FormElements Class


TextBox()

Create TextBox input element in your form.

FormElements::TextBox (String $fieldName,Array $attrb  ) : String 
                        

FileUpload()

Create Submit input element in your form.

FormElements::FileUpload (String $fieldName,Array $attrb  ) : String 
                        

Password()

Create Password input element in your form.

FormElements::Password (String $fieldName,Array $attrb  ) : String 
                        

Submit()

Create Submit input element in your form.

FormElements::Submit (String $fieldName,Array $attrb  ) : String 
                        

Reset()

Create Reset input element in your form.

FormElements::Reset (Array $attrb  ) : String 
                        

Radio()

Create Radio input element in your form.

FormElements::Radio (String $fieldName, String $value, Array $attrb  ) : String 
                        

Checkbox()

Create Checkbox input element in your form.

FormElements::Checkbox (String $fieldName, String $value, Array $attrb  ) : String 
                        

Button()

Create Button input element in your form.

FormElements::Button (String $fieldName, String $value, Array $attrb  ) : String 
                        

ColorDialog()

Create ColorDialog input element in your form.

FormElements::ColorDialog (String $fieldName,Array $attrb  ) : String 
                        

DateSelect()

Create DateSelect input element in your form.

FormElements::DateSelect (String $fieldName,Array $attrb  ) : String 
                        

Email()

Create Email input element in your form.

FormElements::Email (String $fieldName,Array $attrb  ) : String 
                        

Month()

Create Month input element in your form.

FormElements::Month (String $fieldName,Array $attrb  ) : String 
                        

Numeric()

Create Numeric input element in your form.

FormElements::Numeric (String $fieldName,Array $attrb  ) : String 
                        

SearchBox()

Create Email input element in your form.

FormElements::SearchBox (String $fieldName,Array $attrb  ) : String 
                        

Phone()

Create Phone input element in your form.

FormElements::Phone (String $fieldName,Array $attrb  ) : String 
                        

Url()

Create Url input element in your form.

FormElements::Url (String $fieldName,Array $attrb  ) : String 
                        

Time()

Create Time input element in your form.

FormElements::Time (String $fieldName,Array $attrb  ) : String 
                        

Label()

Create Label element in your form.

FormElements::Label (String $text,String $labelFor ,Array $attrb  ) : String 
                        




CSRF Protection

For enabling csrf protection on your forms please add csrfField tag to your form. Below is a example for using a csrf protection field in your form.
view\Register.php


    Usage 
FormElements::csrfField() : String

Example
//------------------------------------------------------------------------
use Ness\UI\Page; use Ness\Url as pUrl; use Ness\Forms\Form; use Ness\Forms\FormElements; $view = new Page(); $view->SetLayout("master.php"); $view->SetParameter("pageHeader", "TEST PAGE"); $view->BeginContent(); #------------------------------------------------------------------------------- echo "User Login"; $frm = new Form("register"); echo $frm->DefineForm(array("method"=>"POST")); $frm->SetModel("userModel"); $frm->Call("btnClick_Register"); echo $frm->setElement(FormElements::csrfField()); echo $frm->setElement(FormElements::TextBox("username", array("class"=>"form-control", "placeholder"=>"Username"))); echo $frm->setElement(FormElements::Password("pwd", array("class"=>"form-control", "placeholder"=>"****"))); echo $frm->FinishForm(); #------------------------------------------------------------------------------- $view->EndContent();




XSS Protection

You can use setProtected(true) function for enabling or disabling XSS protection enabled, by default it is enabled.


    $frm = new Form("testform");
...
..
.
$frm->setProtected($status);
$status TRUE | FALSE


Example use in forms
view\Register.php

                                    
    use Ness\UI\Page;
    use Ness\Url as pUrl;
    use Ness\Forms\Form;
    use Ness\Forms\FormElements;
    
    
    $view = new Page();
    $view->SetLayout("master.php");
    $view->SetParameter("pageHeader", "TEST PAGE");
    $view->BeginContent();
    #-------------------------------------------------------------------------------
    
        echo "User Login";
        $frm = new Form("register");
        echo $frm->DefineForm(array("method"=>"POST"));
             $frm->SetModel("userModel");
             $frm->setProtected(TRUE); // $frm->setProtected(FALSE);
             $frm->Call("btnClick_Register");
    
        echo $frm->setElement(FormElements::csrfField());
        echo $frm->setElement(FormElements::TextBox("username", array("class"=>"form-control", "placeholder"=>"Username")));
        echo $frm->setElement(FormElements::Password("pwd", array("class"=>"form-control", "placeholder"=>"****")));
        echo $frm->FinishForm();
    
    #-------------------------------------------------------------------------------
    $view->EndContent();