Skip to content

Commit 5c19b4d

Browse files
committed
patch for v1.0
1 parent 6b611c9 commit 5c19b4d

18 files changed

+2080
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

README.md

Lines changed: 372 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,372 @@
1-
# octaValidate-PHP
2-
This PHP Library helps to validate your forms using regular expressions & validation rules.
1+
# <img align="center" src="https://octagon-simon.github.io/octaValidate/img/ov-success.png" width="25px"> octaValidate-PHP V1.0
2+
3+
This library helps to validate your forms server-side using validation rules and sophisticated regular expressions.
4+
5+
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.
6+
7+
This Library also helps to validate your frontend forms using JavaScript. [Visit the repository](https://github.com/Octagon-simon/octaValidate)
8+
9+
## DOCUMENTATION
10+
11+
Visit the [DOCUMENTATION](https://octagon-simon.github.io/projects/octavalidate/php/) to learn more about this GREAT Library, and play with the forms there!
12+
13+
## INSTALL
14+
15+
### COMPOSER
16+
17+
```shell
18+
$ composer require simon-ugorji/octavalidate-php
19+
```
20+
21+
### LOCAL
22+
23+
- Download and import the latest release to your project.
24+
- In your project, use the require keyword & include the file **Validate.php**
25+
- 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**.
26+
27+
```php
28+
require 'src/Validate.php';
29+
30+
use Validate\octaValidate;
31+
32+
$myForm = new octaValidate('FORM_ID');
33+
```
34+
35+
## How to Use
36+
37+
- 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*.
38+
39+
- Define validation rules for the form inputs
40+
- Invoke the `validate()` method and pass in the rules as an argument.
41+
42+
```php
43+
//require the library
44+
require 'src/Validate.php';
45+
46+
use Validate\octaValidate;
47+
48+
//create new instance
49+
$myForm = new octaValidate('FORM_ID');
50+
51+
//syntax for defining validation rules
52+
$valRules = array(
53+
"FORM_INPUT_NAME" => array(
54+
["RULE_TITLE", "CUSTOM_ERROR_MESSAGE"]
55+
)
56+
);
57+
58+
/* If you don't provide a custom error message,
59+
the script will use the default one available
60+
*/
61+
62+
//define rules for each form input name
63+
$valRules = array(
64+
"uname" => array(
65+
["R", "Your username is required"]
66+
),
67+
"email" => array(
68+
["R", "Your Email Address is required"],
69+
["EMAIL", "Your Email Address is invalid!"]
70+
) );
71+
72+
//begin validation
73+
if ($myForm->validate($valRules) === true){
74+
//process form data here
75+
}else{
76+
//return errors
77+
$myForm->getErrors();
78+
}
79+
```
80+
The validate method returns a `boolean`.
81+
82+
- true means there are no validation errors
83+
- false means there are validation errors
84+
85+
> Make sure that all input elements have a **unique name**.
86+
87+
## VALIDATION RULES
88+
89+
Here are the inbuilt validation rules.
90+
91+
- R - A value is required.
92+
- ALPHA_ONLY - The value must be letters only! (lower-case or upper-case).
93+
- LOWER_ALPHA - The value must be lower-case letters only.
94+
- UPPER_ALPHA - The value must be upper-case letters only.
95+
- ALPHA_SPACES - The value must contain letters or Spaces only!
96+
- ALPHA_NUMERIC - The value must contain letters and numbers.
97+
- DATE_MDY - The value must be a valid date with the format mm/dd/yyyy.
98+
- DIGITS - The value must be valid digits or numbers.
99+
- PWD - The value must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters.
100+
- EMAIL - The value must be a valid Email Address.
101+
- URL - The value must be a valid URL
102+
- URL_QP - The value must be a valid URL and may contain Query parameters.
103+
- USERNAME - The value may contain letters, numbers, a hyphen or an underscore.
104+
- TEXT - The value may contain any of these special characters (. , / () [] & ! '' "" : ; ?)
105+
106+
Didn't see the validation rule that you need for your form? Don't worry!
107+
108+
With this library, you have the power to define your custom rule and it will be processed as if it were an inbuilt rule.
109+
110+
## CUSTOM VALIDATION RULES
111+
112+
Syntax for defining a custom rule
113+
114+
```php
115+
$myForm->customRule($RULE_TITLE, $REG_EXP, $ERROR_TEXT);
116+
```
117+
Here's a custom rule to validate a password.
118+
119+
```php
120+
//require the library
121+
require 'src/Validate.php';
122+
123+
use Validate\octaValidate;
124+
125+
//create new instance
126+
$myForm = new octaValidate('my_form');
127+
128+
//custom password validation
129+
$rule_title = 'PASS';
130+
$reg_exp = '/12345/';
131+
$err_txt = "Please enter 12345";
132+
//build the rule
133+
$myForm->customRule($rule_title, $reg_exp, $err_txt);
134+
135+
//provide the rule title when defining validation rules
136+
$valRules = array(
137+
"password" => array(
138+
["PASS"]
139+
)
140+
);
141+
```
142+
143+
## MORE CUSTOM RULES
144+
145+
What if you want to define more validation rules?
146+
147+
All you have to do is to 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.
148+
149+
Here's the syntax
150+
```php
151+
$RULES = array(
152+
"RULE_TITLE" => ['REG_EXP', 'CUSTOM_ERROR_MESSAGE']
153+
);
154+
$myForm->moreCustomRules($RULES);
155+
```
156+
Here's a sample rule username & password rules
157+
```php
158+
//require the library
159+
require 'src/Validate.php';
160+
161+
use Validate\octaValidate;
162+
163+
$myForm = new octaValidate('my_form');
164+
165+
//custom username & password validation
166+
$rules = array(
167+
"PASS" => ['/12345/', "Please enter 12345"],
168+
"UNAME" => ['/simon/', "Please enter simon"]
169+
);
170+
171+
//build the rule
172+
$myForm->moreCustomRules($rules);
173+
174+
//provide the rule title when defining validation
175+
$valRules = array(
176+
"username" => array(
177+
["UNAME"]
178+
),
179+
"password" => array(
180+
["PASS"]
181+
)
182+
);
183+
```
184+
> Note that: All Rule Titles are **case-sensitive!**
185+
186+
## ATTRIBUTES VALIDATION
187+
188+
Currently we have 4 types of attribute validation:
189+
190+
- length validation
191+
- EqualTo validation
192+
- Size validation
193+
- File Validation
194+
195+
All attribute validation follows the syntax below
196+
197+
```php
198+
//syntax
199+
$valRules = array(
200+
"FORM_INPUT_NAME" => array(
201+
["ATTRIBUTE_TITLE", "VALUE", "CUSTOM_ERROR_MESSAGE"]
202+
)
203+
);
204+
```
205+
### LENGTH VALIDATION
206+
207+
You can check the number of characters provided by the user using this validation.
208+
209+
So we have;
210+
211+
- maxlength (5) - This means that value must be 5 characters or less.
212+
- minlength (5) - This means that value must be up to 5 characters or more.
213+
- length (5) - This means that value must be equal to 5 characters.
214+
215+
For Example;
216+
217+
```php
218+
//sample validation
219+
$valRules = array(
220+
"username" => array(
221+
["R", "Your username is required"],
222+
["MINLENGTH", "5", "Your username must contain 5 characters or more"]
223+
),
224+
"age" => array(
225+
["R", "Your age is required"],
226+
["LENGTH", "2", "Your Age must contain 2 digits!"]
227+
)
228+
);
229+
```
230+
### EQUALTO VALIDATION
231+
232+
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.
233+
234+
```php
235+
//sample validation
236+
$valRules = array(
237+
"password" => array(
238+
["R", "Your password is required"],
239+
["EQUALTO", "confirm_password", "Both passwords do not match"]
240+
)
241+
);
242+
```
243+
### FILE VALIDATION
244+
245+
You can validate: **accept, accept-mime, size, minsize, maxsize** for an uploaded file or multiple uploaded files.
246+
247+
- ACCEPT - Use this attribute to list out the file extensions allowed for upload
248+
- ACCEPT-MIME - Use this attribute to list out the file MIME types allowed for upload. It supports a wildcard eg audio/*
249+
- SIZE (2mb) - This means that the file or files provided must be 2mb in size
250+
- MINSIZE (5mb) - This means that the file or files provided must be up to 5mb or more.
251+
- MAXSIZE (5mb) - This means that the file or files provided must be 5mb or less.
252+
253+
> Note that **size, minsize & maxsize** works on single or multiple file upload.
254+
255+
For example;
256+
```php
257+
//sample validation
258+
$formRules = array(
259+
//single file upload
260+
"file" => array(
261+
["R"],
262+
["ACCEPT", ".mp3, .ogg"],
263+
["MAXSIZE", "5mb"]
264+
),
265+
//multiple files upload
266+
"files" => array(
267+
["R"],
268+
["ACCEPT-MIME", "image/*"],
269+
["MAXFILES", "5"],
270+
["MAXSIZE", "50mb"]
271+
)
272+
);
273+
```
274+
Please refer to the [documentation](https://octagon-simon.github.io/projects/octavalidate/php/file.html) to learn more about validating a file input.
275+
276+
## API METHODS
277+
278+
### STATUS
279+
280+
You can invoke the **getStatus()** method anytime to check the number of validation errors on the form.
281+
282+
```php
283+
//require the library
284+
require 'src/Validate.php';
285+
286+
use Validate\octaValidate;
287+
288+
//create new instance
289+
$myForm = new octaValidate('my_form');
290+
291+
//return error count
292+
$myForm->getStatus();
293+
```
294+
295+
## CONFIGURATION
296+
297+
We have 3 configuration options:
298+
299+
- stripTags: <code>Boolean</code>
300+
301+
Just like PHP's inbuilt `stripTags` function, this option loops through the POST Array and removes anything enclosed within a tag. Default value is `false`.
302+
303+
- strictMode: <code>Boolean</code>
304+
305+
This option removes extra white space from the start and at the end of a form input and also prevents the user from providing reserved keywords as values. Default value is **false**.
306+
- strictWords: <code>Array</code>
307+
308+
This option alows you to provide words that users are not supposed to submit. For eg ["null", "error", "false"]. In order to use this option, you must set **strictMode** to **true**.
309+
310+
To use any of these options, provide it as an array and pass it as the second argument when creating an instance of octaValidate.
311+
312+
```javascript
313+
//require the library
314+
require 'src/Validate.php';
315+
316+
use Validate\octaValidate;
317+
318+
//set configuration
319+
$options = array(
320+
"stripTags" => true,
321+
"strictMode" => true,
322+
"strictWords" => ["null", "undefined"]
323+
);
324+
//create new instance
325+
$myForm = new octaValidate('FORM_ID', $options);
326+
```
327+
328+
## REFERENCE METHODS
329+
After creating a new instance of the function, the methods below becomes available for use.
330+
331+
```javascript
332+
//create instance of the function
333+
const myForm = new octaValidate('FORM_ID');
334+
```
335+
336+
- **validate()**
337+
338+
Invoke this method to begin validation
339+
- **getStatus()**
340+
341+
Invoke this method to see the number of validation errors on a form
342+
- **getForm()**
343+
344+
This method returns the form ID.
345+
- **customRule(RULE_TITLE, REG_EXP, ERROR_TEXT)**
346+
347+
Invoke this method to define your custom validation rule.
348+
- **moreCustomRules(RULES)**
349+
350+
Invoke this method to define more custom validation rules.
351+
- **getVersion()**
352+
353+
Invoke this method to retrieve the library's version number.
354+
355+
> There are more methods in the documentation, Please refer to the [documentation](https://octagon-simon.github.io/projects/octavalidate/php/api.html) to learn more.
356+
357+
358+
## DEMO
359+
360+
- 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.
361+
362+
## Author
363+
364+
[Simon Ugorji](https://twitter.com/ugorji_simon)
365+
366+
## Support Me
367+
368+
[Donate with PayPal](https://www.paypal.com/donate/?hosted_button_id=ZYK9PQ8UFRTA4)
369+
370+
## Contributors
371+
372+
[Simon Ugorji](https://twitter.com/ugorji_simon)

0 commit comments

Comments
 (0)