mahmoud-abdelfadeil / validator-filter-php
package to facilitate validation and filtering in PHP
This package is auto-updated.
Last update: 2024-09-19 08:44:57 UTC
README
package to facilitate validation and filtering in PHP
Created By : Mahmoud Abdelfadeil
install
Confige File
Validator
- Syntax
- NotesSyntax
- string
- url
- required
- options
- ip
- email
- max_length
- min_length
- max_val
- min_val
- uniqueAndExists
- unique
- exists
Filter
-string
-num_int
-num_float
-email
-url
-encoded
-magic_quotes
-special_char
installation
1- composer require mahmoud-abdelfadeil/validator-filter-php
ConfigeFile
create config file Through the following command :
php vendor/mahmoud-abdelfadeil/validator-filter-php/src/Validator/create-file-config.php
Or create a config file by writing these lines into the composer.json file , Then command composer dump-autoload
{ "scripts": { "post-autoload-dump": [ "php vendor/mahmoud-abdelfadeil/validator-filter-php/src/Validator/create-file-config.php" ] } }
Then the file is created config/validator-filter-config.php
return [ // config database "db_host"=>"localhost", "db_connection"=>"mysql", "db_name"=>"filter-validate-php", "db_user"=>"root", "db_password"=>"", // config langauge [ar - en] "lang"=>'en' ];
It is a settings file that makes settings for Database and language settings
Validate
It is intended to verify the value
There are several functions of them
Syntax
/* $arr=[ request name = > rules ] */ // example include 'vendor/autoload.php'; $validate=new ValidatorFilterPHP\ValidatorPHP(); $rules=[ 'name'=>'required|string|max_length:100|min_length:5', 'age'=>'required|number|max_val:60|min_val:10', 'email'=>'required|unique:users,email' ]; if(isset($_POST['submit'])){ $validate->Validator($rules); if($validate->has_error_validate()){ echo "<pre>"; print_r( $validate->has_error_validate()); }else{ // next request } }
NotesSyntax
As we noticed in the previous example, we write the name of the request, then the rules to be verified, and each rule is separated by using the ' | '
There are many rules that we need to write a value, for example the max_length, which means the minimum number of characters, and the rule and value are separated using the ' : '
Rules
string
Check the value if it is textual or not
number
Check if the value is a number or not
url
Check if the value is a url or not
required
Check if the value is empty or not
options
It forces the user to enter the specified values into the options
From a rule that takes specified values but is written this way :
options:[option 1 , option 2 , .......]
// example 'category'=>'required|options:[car,laptop,mobile]'
ip
Check if the value is a ip or not
Check if the value is a email or not
max_length
Checks the maximum amount of characters allowed
min_length
Check the minimum number of characters allowed
example
// maximum length 100 characters // minimum length 5 characters 'name'=>'required|max_length:100|min_length:5'
max_val
Check the maximum allowed value
min_val
Check the minimum allowed value
// maximum value 60 // minimum value 20 'age'=>'required|max_val:60|min_val:20'
**Note:The max_length or min_length is used with the string. The max_val and min_val are used with numbers . In addition, they check if it is a number or not**
uniqueAndExists
Before dealing with either of them, make the database settings from the config/validator-filter-config.php
return [ // config database "db_host"=>"localhost", "db_connection"=>"mysql", "db_name"=>"filter-validate-php", "db_user"=>"root", "db_password"=>"", // config langauge "lang"=>'en' ];
unique
Verify that the value is not present in the data base
Synax :
unique:table,column
// example 'email'=>'required|unique:users,email'
Here, verify that email does not already exist in Databases
**If modified, the identifier is sent with the unique identifier to check all rows except for the sender and write in this way The id field name for example tbl_id and then the value that way tbl_id = 5**
// example if update data 'email'=>'required|unique:users,email,tbl_id=5'
If the field is named id, you only need to enter the value
'email'=>'required|unique:users,email,5'
exists
Synax :
exists:table,column
Verify that the value is present in the data base
// example 'email'=>'required|unique:users,email'
Filters
a data filter
Syntax :
$obj->functionName($data)
// example $filter=new ValidatorFilterPHP\FilterPHP(); $str="<script>mahmoud abdelfadeil</script>"; $str_filter = $filter->string($str); echo $str_filter ; // output = mahmoud abdelfadeil
functions
string
$str="<script>mahmoud abdelfadeil</script>"; $str_filter = $filter->string($str); echo $str_filter ; // output = mahmoud abdelfadeil
num_int
number integer
$data="<script>mahmoud 1299 abdelfadeil</script>"; $data_filter = $filter->num_int($data); echo $data_filter ; // output = 1299
num_float
number float
$data="12.99"; $data_filter = $filter->num_float($data); echo $data_filter ; // output = 1299
However, this function takes a second argument, which is the flag
Flags = e or E or , or .
$data="12.99"; $data_filter = $filter->num_float($data,'.'); echo $data_filter ; // output = 12.99
$data="12,99"; $data_filter = $filter->num_float($data,','); echo $data_filter ; // output = 12,99
$data_1="12ee9e9"; $data_1_filter = $filter->num_float($data_1); echo $data_1_filter ; // output data 1 = 1299 $data_2="12ee9e9E"; $data_2_filter = $filter->num_float($data_2,'e'); echo $data_2_filter ; // output data 2 = 12ee9e9E $data_3="12ee9e9E"; $data_3_filter = $filter->num_float($data_3,'E'); echo $data_3_filter ; // output data 3 = 12ee9e9E
$data="mahmoud . €¶€€¶€€¶€ abdelfadeil@test.test"; $data_filter = $filter->email($data); echo $data_filter ; // output data = mahmoud.abdelfadeil@test.test
url
$data="mahmoud-abdelfadeil.€¶€¶€me"; $data_filter = $filter->url($data); echo $data_filter ; // output data = mahmoud-abdelfadeil.me
encoded
$data="mahmoud-abdelfadeil.€¶€¶€me"; $data_filter = $filter->encoded($data); echo $data_filter ; // output data = mahmoud-abdelfadeil.%E2%82%AC%C2%B6%E2%82%AC%C2%B6%E2%82%ACme
magic_quotes
$data="mahmoud's here"; $data_filter = $filter->magic_quotes($data); echo $data_filter ; // output data = mahmoud\'s here
special_char
$data="<b>mahmoud abdelfadeil</b>"; $data_filter = $filter->special_char($data); echo $data_filter ; // output data = <b>mahmoud abdelfadeil</b>