VALIDATE FIELDS METHOD
Invoke this method and pass in your validation rules as the first argument then the form fields as the second argument to begin validation on the form fields.
The second argument of validateFields
method could be fields from the $_POST
array or fields from the $_GET
array. It defaults to the $_POST
array if no value is provided.
//require the library
require 'src/Validate.php';
use Validate\octaValidate;
//create new instance
$myForm = new octaValidate('my_form');
//sample validation
$valRules = array(
"username" => array(
["R", "Your username is required"]
)
);
//begin validation on $_POST Array
$myForm->validateFields($valRules, $_POST);
//begin validation on $_GET Array
$myForm->validateFields($valRules, $_GET);
//begin validation on custom Array
$myForm->validateFields($valRules, $myArray);
Return type: boolean
true means that all validation tests passed and false means that one or more tests
failed.
VALIDATE FILES METHOD
Invoke this method then pass in your validation rules as an argument to begin validation on the file upload fields.
//require the library
require 'src/Validate.php';
use Validate\octaValidate;
//create new instance
$myForm = new octaValidate('my_form');
//sample validation
$valRules = array(
"image" => array(
["R", "Your profile picture is required"]
)
);
//begin validation on uploaded files
$myForm->validateFiles($valRules);
Return type: boolean
true means that all validation tests are passed false means that one or more tests
failed.
GET FORM
This method returns the form id attached to the instance.
//require the library
require 'src/Validate.php';
use Validate\octaValidate;
//create new instance
$myForm = new octaValidate('my_form');
//return form id
$myForm->getForm();
Return type: string
CUSTOM RULE
This method allows you to construct a custom rule. To construct a rule, you need the
rule's title, regular expression and an error message.
This method allows you to construct a single rule. Use the multiple
rules method to construct more rules.
//require the library
require 'src/Validate.php';
use Validate\octaValidate;
//create new instance
$myForm = new octaValidate('my_form');
//Here's the syntax
$myForm->customRule($RULE_TITLE, $REG_EXP, $ERROR_TEXT);
//custom password validation
$rule_title = 'PASS';
$reg_exp = '/12345/';
$err_txt = "Please enter 12345";
//build the rule
$myForm->customRule($rule_title, $reg_exp, $err_txt);
//provide the rule title when defining validation
$valRules = array(
"password" => array( ["PASS"] )
);
All Rule Titles are case-sensitive!
MORE CUSTOM RULES
This method allows you to construct more custom rules.
To use this method, create an array that will contain the rule title as the array's
property, and its value is another array containing the rule's regular
expression, and error text separated by a comma.
//require the library
require 'src/Validate.php';
use Validate\octaValidate;
//create new instance
$myForm = new octaValidate('my_form');
//Here's the syntax
$RULES = array(
"RULE_TITLE" => ['REG_EXP', 'CUSTOM_ERROR_MESSAGE']
);
$myForm->moreCustomRules($RULES);
//custom username & password validation
$rules = array(
"PASS" => ['/12345/', "Please enter 12345"],
"UNAME" => ['/simon/', "Please enter simon"]
);
//build the rule
$myForm->moreCustomRules($rules);
//provide the rule title when defining validation
$valRules = array(
"username" => array( ["UNAME"] ),
"password" => array( ["PASS"] )
);
GET ERRORS
This method allows you to return server-side validation errors to the front-end for the user to
see.
Click on the button to learn how you can handle validation errors.
Learn more
if($myForm->validateFields($valRules, $_GET) === true){
//process form data here
}else{
//return errors
print_r(json_encode($myForm->getErrors()));
}
Return type: Array
Make sure to invoke the getErrors()
method,
only when the validate method returns false!
GET VERSION
This method returns the current version of this Great Library.
//require the library
require 'src/Validate.php';
use Validate\octaValidate;
//create new instance
$myForm = new octaValidate('my_form');
//get version
$myForm->getVersion();
Return type: string
GET CREDITS
Invoke this method to return a list of people that contributed to this library.
//require the library
require 'src/Validate.php';
use Validate\octaValidate;
//create new instance
$myForm = new octaValidate('my_form');
//get contributors
$myForm->getCredits();
Return type: Array