Fork me on GitHub

Application - Form Validator

#Form Validator

#Feature

Validation is a method to check the validity of an input and give feedback. It significantly enhances to remove vulnerability. This document explains inline form validation and error handling design techniques.

#Support Rules

  • number
  • string
  • email
  • int
  • float
  • ip
  • url
  • json
  • array

#How To Use

#COMMON


$testValue = "abcd";
$p = FormValidator::create()->setValidate("p", $testValue)->setRule("number")->validate(true, false);

#RULE

NUMBER


$testValue = "abcd";
$p = FormValidator::create()->setValidate("p", $testValue)->setRule("number")->validate(true, false);

STRING


$testValue = "abcd";
$p = FormValidator::create()->setValidate("p", $testValue)->setRule("number")->validate(true, false);

EMAIL


$testValue = "abcd";
$p = FormValidator::create()->setValidate("p", $testValue)->setRule("number")->validate(true, false);

INT


$testValue = "abcd";
$p = FormValidator::create()->setValidate("p", $testValue)->setRule("number")->validate(true, false);

FLOAT


$testValue = "abcd";
$p = FormValidator::create()->setValidate("p", $testValue)->setRule("number")->validate(true, false);

IP


$testValue = "abcd";
$p = FormValidator::create()->setValidate("p", $testValue)->setRule("number")->validate(true, false);

URL


$testValue = "abcd";
$p = FormValidator::create()->setValidate("p", $testValue)->setRule("number")->validate(true, false);

JSON


$testValue = "abcd";
$p = FormValidator::create()->setValidate("p", $testValue)->setRule("number")->validate(true, false);

ARRAY


$testValue = "abcd";
$p = FormValidator::create()->setValidate("p", $testValue)->setRule("number")->validate(true, false);

#Full Example

# index.php


define("PROJECT_SERVER_NAME", "GAME SERVER"); // Whatever you want to put
define("PROJECT_SERVER_TYPE", "develop"); // Whatever you want to put
define("PROJECT_LOG_LEVEL", "debug"); //
define("PROJECT_PATH", "/home/projects/my_project/"); //this is not document root, same folder as composer
define("PROJECT_PATH_LOG", "/home/projects/my_project/logs/"); //mkdir this folder and chmod 777
define("PROJECT_PATH_VENDOR", "/home/projects/my_project/vendor/");

require_once(PROJECT_PATH_VENDOR . "autoload.php");
require_once(PROJECT_PATH . "/public_html/MyController.php");


class TestRoute extends \CafeLatte\Core\BaseRoute implements \CafeLatte\Interfaces\RouterInterface
{
    public function routing()
    {

        $this->router->get("", function () {
            $this->result = (new MyController($this->request, $this->log))->getHelloWorld();
        });

        $this->router->get("test01", function () {
            $this->result = (new MyController($this->request, $this->log))->getHelloWorld01();
        });

        $this->router->get("test02", function () {
            $this->result = (new MyController($this->request, $this->log))->getHelloWorld02();
        });

        $this->router->get("test03", function () {
            $this->result = (new MyController($this->request, $this->log))->getHelloWorld03();
        });


        //DO NOT REMOVE
        $this->router->run();
    }

}

$framework = new TestRoute();
$framework->execute();

# Example Controller Code(MyController.php)


use CafeLatte\Core\Controller;
use CafeLatte\Interfaces\ControllerInterface;
use CafeLatte\Interfaces\HttpRequestInterface;
use CafeLatte\Interfaces\LoggerInterface;

class MyController extends Controller implements ControllerInterface
{

    public function __construct(HttpRequestInterface $request, LoggerInterface $log)
    {
        parent::__construct($request, $log);
    }

    /**
     * sample code get method of controller
     */
    public function getHelloWorld()
    {
        $number = $this->request->get('number');
        $number = FormValidator::create()->setValidate("number", $number)->setRule("string")->validate(false, true);

        echo "NUMBER : " . $number;
    }


    /**
     * sample code get method of controller
     */
    public function getHelloWorld01()
    {
        $number = FormValidator::create()->setValidate("number", $this->request->get('number'))->setRule("int")->validate(true, true);

        echo "NUMBER : " . $number;
    }

    /**
     * sample code get method of controller
     */
    public function getHelloWorld02()
    {
        $number = FormValidator::create()->setValidate("number", $this->request->get('number'))->setDefaultValue("10000")->setRule("int")->validate(true, true);

        echo "NUMBER : " . $number;
    }

    /**
     * sample code get method of controller
     */
    public function getHelloWorld03()
    {
        $number = FormValidator::create()->setValidate("number", $this->request->get('number'))->doProtectXssInject()->setDefaultValue("string")->setRule("string")->validate();

        echo "NUMBER : " . $number;
    }

}

# Download Sample File

Click the 'Download' button or hit the full url

TAR Package(sample_formvalidator.tar.gz) MD5 : 8d38f01d339a52c9cbfc034d883d5ab6 Download

You can download zip and unzip it. You can see follow folders and files below

├── composer.json
├── logs
├── uploads
└── public_html
    └── .htaccess
    └── index.php
    └── MyController.php