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.

#Function List('red' color is mandatory)

  • Function setValidate(string $key, string $value) : $this
  • Function setRule(string $rule) : $this
    • number
    • string
    • email
    • int
    • float
    • ip
    • url
    • json
    • array
  • Function validate(bool true, bool false)
  • Function doProtectXssInject(string $rule = "high) : $this
    • high
    • normal
    • low
  • Function setReplaceValue($key, string $value) : $this - to replace
  • Function setMinMaxLength(int $min, int $max) : $this - to check length of string parameter, It is NOT range of min/mix value

#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


<?php

require_once("/home/projects/my_project/autoload.php");
require_once("/home/projects/my_project/controller.php");

class MyTestIndex 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 MyTestIndex("/home/projects/my_project/cafelatte.json");
$framework->execute();

# Example Controller Code(MyController.php)


<?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;
    }

}

#Tutorial

To get more information, move to tutorial page, Click `Here`