Skip to content

Commit

Permalink
patch for v1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Octagon-simon committed Aug 10, 2022
1 parent 4e1c929 commit 3d8f1dd
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 37 deletions.
55 changes: 25 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# <img align="center" src="https://octagon-simon.github.io/octaValidate/img/ov-success.png" width="25px"> octaValidate-PHP V1.0
# <img align="center" src="https://octagon-simon.github.io/octaValidate/img/ov-success.png" width="25px"> octaValidate-PHP V1.1

This library helps to validate your forms server-side using validation rules and sophisticated regular expressions.
This is a feature-rich Library that helps to validate your forms server-side using sophisticated regular expressions, PHP's inbuilt validation, and validation rules.

We have included a demo folder containing some forms with validation rules applied to each of them. Open any of the files in this folder in your browser and try to submit the form.
We have included a demo folder containing some forms with validation rules applied to each of them. Open any of the files in your local server and submit the form.

This Library also helps to validate your frontend forms using JavaScript. [Visit the repository](https://github.com/Octagon-simon/octaValidate)

Expand All @@ -14,7 +14,7 @@ Visit the [DOCUMENTATION](https://octagon-simon.github.io/projects/octavalidate/

### COMPOSER

```shell
```
$ composer require simon-ugorji/octavalidate-php
```

Expand All @@ -29,13 +29,11 @@ require 'src/Validate.php';

use Validate\octaValidate;

$myForm = new octaValidate('FORM_ID');
$myForm = new octaValidate('FORM_ID', 'CONFIG_OPTIONS');
```

## 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*.

- Define validation rules for the form inputs
- Invoke the `validate()` method and pass in the rules as an argument.

Expand All @@ -46,7 +44,7 @@ require 'src/Validate.php';
use Validate\octaValidate;

//create new instance
$myForm = new octaValidate('FORM_ID');
$myForm = new octaValidate('FORM_ID', 'CONFIG_OPTIONS');

//syntax for defining validation rules
$valRules = array(
Expand Down Expand Up @@ -79,8 +77,8 @@ if ($myForm->validate($valRules) === true){
```
The validate method returns a `boolean`.

- true means there are no validation errors
- false means there are validation errors
- `true` means there are no validation errors
- `false` means there are validation errors

> Make sure that all input elements have a **unique name**.
Expand Down Expand Up @@ -153,7 +151,8 @@ $RULES = array(
);
$myForm->moreCustomRules($RULES);
```
Here's a sample rule username & password rules
Here's a sample custom username & password validation rule

```php
//require the library
require 'src/Validate.php';
Expand Down Expand Up @@ -206,8 +205,6 @@ $valRules = array(

You can check the number of characters provided by the user using this validation.

So we have;

- maxlength (5) - This means that value must be 5 characters or less.
- minlength (5) - This means that value must be up to 5 characters or more.
- length (5) - This means that value must be equal to 5 characters.
Expand All @@ -229,7 +226,7 @@ $valRules = array(
```
### EQUALTO VALIDATION

You can check if two inputs contain the same values, using the attribute **equalto** on the input name, with a value containing the name of the other input element to check against.
You can check if two inputs contain the same values using the rule **EQUALTO**. The value of this validation rule must be the other **input name** you wish to check against.

```php
//sample validation
Expand All @@ -242,17 +239,19 @@ $valRules = array(
```
### FILE VALIDATION

You can validate: **accept, accept-mime, size, minsize, maxsize** for an uploaded file or multiple uploaded files.
Within File validation, we have rules such as;

- ACCEPT - Use this attribute to list out the file extensions allowed for upload
- ACCEPT-MIME - Use this attribute to list out the file MIME types allowed for upload. It supports a wildcard eg audio/*
- SIZE (2mb) - This means that the file or files provided must be 2mb in size
- MINSIZE (5mb) - This means that the file or files provided must be up to 5mb or more.
- MAXSIZE (5mb) - This means that the file or files provided must be 5mb or less.
- ACCEPT - Use this rule to list out the file extensions allowed for upload. Eg. .png, .jpeg.
- ACCEPT-MIME - Use this rule to list out the file MIME types allowed for upload. It supports a wildcard. Eg audio/*, image/*
- SIZE - This rule makes sure that the size of the file or files provided must be equal to the specified value.
- MINSIZE - This rule makes sure that the size of the file or files provided must be up to the specified value or more.
- MAXSIZE - This rule makes sure that the size of the file or files provided must be the specified value or less.
- FILES, MINFILES, MAXFILES

> Note that **size, minsize & maxsize** works on single or multiple file upload.
> Note that **size, minsize & maxsize** works on both single or multiple file upload.
For example;

```php
//sample validation
$formRules = array(
Expand All @@ -271,13 +270,13 @@ $formRules = array(
)
);
```
Please refer to the [documentation](https://octagon-simon.github.io/projects/octavalidate/php/file.html) to learn more about validating a file input.
Please refer to the [documentation](https://octagon-simon.github.io/projects/octavalidate/php/file.html) to learn more file validation rules.

## API METHODS

### STATUS

You can invoke the **getStatus()** method anytime to check the number of validation errors on the form.
Invoke the **getStatus()** method to return the number of validation errors on the form.

```php
//require the library
Expand Down Expand Up @@ -309,7 +308,7 @@ We have 3 configuration options:

To use any of these options, provide it as an array and pass it as the second argument when creating an instance of octaValidate.

```javascript
```php
//require the library
require 'src/Validate.php';

Expand All @@ -328,9 +327,9 @@ $myForm = new octaValidate('FORM_ID', $options);
## REFERENCE METHODS
After creating a new instance of the function, the methods below becomes available for use.

```javascript
```php
//create instance of the function
const myForm = new octaValidate('FORM_ID');
$myForm = new octaValidate('FORM_ID');
```

- **validate()**
Expand Down Expand Up @@ -359,10 +358,6 @@ const myForm = new octaValidate('FORM_ID');

- Open any file within the demo folder and submit the form or visit the [documentation](https://octagon-simon.github.io/projects/octavalidate/php/) and submit the forms there.

## PHP VERSION

It supports PHP Version from 5.6.40 to the latest.

## Author

[Simon Ugorji](https://twitter.com/ugorji_simon)
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simon-ugorji/octavalidate-php",
"description": "This Library helps to validate your forms server-side using sophisticated regular expressions and validation rules.",
"description": "This is a feature-rich Library that helps to validate your forms server-side using sophisticated regular expressions, PHP's inbuilt validation, and validation rules.",
"type": "library",
"keywords": [
"forms",
Expand All @@ -9,7 +9,8 @@
"regexp",
"validation-library",
"file-validator",
"length-validator"
"length-validator",
"php-form-validation"
],
"homepage": "https://octagon-simon.github.io/octavalidate/php/index.html",
"license": "MIT",
Expand Down
74 changes: 74 additions & 0 deletions demo/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

//require library
require '../src/Validate.php';
use Validate\octaValidate;

//create new instance of the class
$DemoForm = new octaValidate('form_demo');

//custom rules
$customRules = array(
"UNAME" => ['/simon/', "You must enter simon"],
"PASS" => ['/12345/', "You must enter 12345"]
);

$DemoForm->moreCustomRules($customRules);

//define validation rules
$valRules = array(
"username" => array(
["R", "Your username is required"],
["UNAME"]
),
"email" => array(
["R", "Your Email is required"],
["EMAIL", "Your Email is invalid"]
),
"age" => array(
["R", "Your Age is required"],
["DIGITS", "Your Age must be in digits"],
["LENGTH", "2", "Your age must be 2 digits"]
),
"password" => array(
["R", "Your Password is required"],
["PASS"]
)
);

if ($_POST) {

//begin validation
if ($DemoForm->validate($valRules) === true) {

//process form data here
print('<script> alert("NO VALIDATION ERROR") </script>'); }
else {
//retrieve & display errors
print('<script>
window.addEventListener(\'load\', function(){
showErrors(' . $DemoForm->getErrors() . ');
})
</script>');
}
}
?>
<html>
<html>

<body>
<form id="form_demo" method="post" novalidate>
<label>Username</label><br>
<input name="username" type="text" id="inp_uname" value="<?php ($_POST && $_POST['username']) ? print($_POST['username']) : '' ?>"> <br>
<label>Email</label><br>
<input name="email" type="email" id="inp_email" value="<?php ($_POST && $_POST['email']) ? print($_POST['email']) : '' ?>"> <br>
<label>Age</label><br>
<input name="age" type="number" id="inp_age" value="<?php ($_POST && $_POST['age']) ? print($_POST['age']) : '' ?>"> <br>
<label>Password</label><br>
<input name="password" type="password" id="inp_pass" value="<?php ($_POST && $_POST['password']) ? print($_POST['password']) : '' ?>"> <br><br>
<button type="submit">Run Test</button>
</form>
<script src="../frontend/helper.js"></script>
</body>

</html>
10 changes: 5 additions & 5 deletions src/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
namespace Validate;

/**
* OctaValidate Main PHP V1.0
* OctaValidate Main PHP V1.1
* author: Simon Ugorji
* Last Edit : 27th July 2022
* Last Edit : 10th August 2022
*/

//include rules library
Expand All @@ -14,7 +14,7 @@ class octaValidate
//store errors
private static $errors = [];
//version
private static $version = '1.0';
private static $version = '1.1';
//author
private static $author = 'Simon Ugorji';
//form id
Expand Down Expand Up @@ -240,7 +240,7 @@ function ($word) use ($inputValue) {
else if (self::$continueValidation && $rt === "EMAIL" && $inputValue) {
//error message
$errMsg = (!empty($valData[1])) ? $valData[1] : "This Email Address is Invalid";
if (Validate_Email($inputValue) === false) {
if (filter_var($inputValue, FILTER_VALIDATE_EMAIL) === false) {
self::$continueValidation = 0;
self::ovNewError($inputName, $errMsg);
}
Expand Down Expand Up @@ -336,7 +336,7 @@ function ($word) use ($inputValue) {
else if (self::$continueValidation && $rt === "URL" && $inputValue) {
//error message
$errMsg = "Please provide a valid URL that begins with http or https!";
if (Validate_Url($inputValue) === false) {
if (filter_var($inputValue, FILTER_VALIDATE_URL) === false) {
self::$continueValidation = 0;
self::ovNewError($inputName, $errMsg);
}
Expand Down

0 comments on commit 3d8f1dd

Please sign in to comment.