Ratings | | Unique User Downloads | | Download Rankings |
Not enough user ratings | | Total: 184 | | All time: 8,672 This week: 660 |
Version | | License | | PHP version | | Categories |
php-form-validator 1.0.0 | | Free For Educatio... | | 5 | | PHP 5, Validation |
|
Description | | Author |
This class can validate form inputs according to different rules.
It provides different functions that can validate values using different rules and returns 1 or 0 depending on whether the value is valid.
Currently it can validate required values, email addresses, minimum, maximum and exact length, number between a range, letters or digits, check file name extensions, IP addresses, date ranges, etc.. | |
|
Example
<?php
require_once 'class.validator.php';
require_once realpath(__DIR__.'/../class.rules.php');
function validate($funName,$formDataArray){
$formRulesObj = new FormRules();
$validator = new Validations();
$loginRules = $formRulesObj->$funName();
$response = $status = '';
foreach($loginRules['rules'] as $name=>$conds){
foreach($conds as $cond=>$value){
$valueOptions = array();
if(is_array($value)){
$valueOptions = $value;
if($valueOptions[0] === true){
if(is_array($valueOptions[1])){
// print_r($valueOptions[1]);
$options = array('condition'=>$valueOptions[1]);
if(isset($formDataArray[$name])) $status = $validator->$cond($formDataArray[$name],$options);
if($status == 0){
$response[$name][$cond] = $loginRules['messages'][$name][$cond];
}
} else {
$status = $valueOptions[1]($formDataArray[$name]);
if($status == 0){
$response[$name][$cond] = $loginRules['messages'][$name][$cond];
}
}
}
} else if($value === true || $value != ''){
$options = array('condition'=>$value);
if(isset($formDataArray[$name])) $status = $validator->$cond($formDataArray[$name],$options);
if($status == 0){
$response[$name][$cond] = $loginRules['messages'][$name][$cond];
}
}
}
}
if(!empty($response)){
// Set only one error message for each input field
foreach($response as $k=>$resp){
if(is_array($resp) && count($resp) > 1){
$new_res[$k][key($resp)] = $a = array_shift($resp);
} else {
$new_res[$k] = $resp;
}
}
return $errorMsgs = json_encode($new_res);
} else {
return true;
}
}
?>
|
Details
This class validates form at server side with rules and messages by user. It works as Jquery validation plugin fashion.
When click on form submit button, will make a ajax call and send form data. After validate the data with defined rules, display the error messages
to corresponding input fields which are defined as messages.
Features:
- Server side validation, but looks like client side validation
- Easy to define rules and messages for each input field
- Rules and error messages are define in array format
- It Validates all input types including input type 'file'
- Validation Types:
not_empty, min_length, max_length, alphaonly, alphanumonly, splalphanumonly, ip address,regular expressions, file extensions, date_format, min_date, max_date
|
Applications that use this package |
|
No pages of applications that use this class were specified.
If you know an application of this package, send a message to the author to add a link here.