GETTING STARTED

Server-side Validation Library for PHP

Welcome to this GREAT library that helps to validate your forms server-side using validation rules, PHP's inbuilt validation, and sophisticated regular expressions.

We don't store data submitted through the forms on this documentation, we only validate & discard them immediately.


INSTALLATION

COMPOSER
$ composer require simon-ugorji/octavalidate-php

LOCAL
  • Download and import the latest release to your project.
  • In your project, use the require keyword & include the file Validate.php
  • Now with the use keyword, link the class to your project and create a new instance of the class by passing in the form id as the first argument and any configuration as the second argument.
require 'src/Validate.php';
use Validate\octaValidate;
$myForm = new octaValidate('FORM_ID');
Download

HOW TO USE

Create a new instance of the class octavalidate, and pass in the form ID as the first argument, then any configuration as the second argument.

To begin validation, define validation rules for the form fields, then pass in an array of the validation rules as the first argument to the validateFields method, and an array of the form fields as the second argument. It defaults to the $_POST array if no value is provided.

The second argument of validateFields method could be fields from the $_POST array or fields from the $_GET array.

//require the library
require 'src/Validate.php';
use Validate\octaValidate;
//set configuration
$options = array(
"stripTags" => true,
"strictMode" => true
);
//create new instance
$myForm = new octaValidate('my_form', $options);
//define rules for each form input name
$valRules = array(
"uname" => array(
["R", "Your username is required"]
),
"email" => array(
["R", "Your Email Address is required"],
["EMAIL", "Your Email Address is invalid!"]
)
);
//begin validation on form fields from $_POST array
if ($myForm->validateFields($valRules, $_POST) === true){
//process form data here
}else{
//return errors
print_r(json_encode($myForm->getErrors()));
}

The validate method returns a boolean.

  • true means there are no validation errors
  • false means there are validation errors

FEATURES

  • You have a rich set of validation rules at your disposal.
  • You can choose to validate fields from the $_POST array or fields from the $_GET array.
  • You can validate anything submitted through the form, including uploaded files.
  • Validation errors are well grouped based on their form fields.
  • You have the ability to prevent a user from submitting certain values through the form
  • Form fields are well validated to ensure that only the correct values are allowed