Download the PHP package hispanicode/validation without Composer
On this page you can find all versions of the php package hispanicode/validation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hispanicode/validation
More information about hispanicode/validation
Files in hispanicode/validation
Package validation
Short Description Form validation model with fantastic tools for the client and server sides with php and jQuery.
License GPL3
Homepage https://github.com/hispanicoce/validation
Informations about the package validation
validation
This class can be used to validate forms on the browser and server sides.
It can take an array of validation rules and generates JavaScript to perform validation on the browser side of given form fields.
The class can generate validation JavaScript code using jQuery that is performed for different form events like when the form input is changed, the form loses focus, when a key is released, etc..
It can also perform validation of the submitted form input on the server side with the class PHP code. Currently in can perform validation types like: required value, minimum length, maximum length, required file, file minimum and maximum size, file MIME type, image maximum width and height, checked checkbox, valid date and time format, IP address, valid email address, valid URL, match regular expression, maximum and minimum length, value equal to another input, valid number, minumum and maximum number, etc..
Important: for client-side validation it is necessary to include jQuery and file validation.js
Install with composer
composer require hispanicode/validation
Simple usage
Require the class file
require "src/Validation.php";
/*
if install with composer
require "vendor/autoload.php";
*/
New instance
use Hispanic\Validation;
$validation = new Validation();
Rules
$rules = array(
"name" => "required|name|min_length:3|max_length:50",
"email" => "required|email|min_length:6|max_length:80",
"password" => "required|between:6-30",
"confirm_password" => "required|equalsTo:password",
"terms" => "checked",
);
Optional. Custom messages. For example translate in other language (French)
$messages = array(
"name.required" => "Le champ :attribute est obligatoire",
/* ... */
);
The default messages in distinct languages is possible in the language.php file. The spanish and english translates are availables but you can translate in your language.
//The default translate is "en" in this case not is need use the translate method.
//Translate in spanish
$validation->translate("es");
In the client validation is possible handle javascript events
$events = array(
"name" => "keyup|blur|change",
/* ... */
);
Start the client validation
//The $messages and $events params are optionals
$validation->client($rules, $messages, $events);
Generate the client validation is easy
//The argument is the id of the form
echo $validation->getClientValidation("#form");
For get the client messages set the next structure based in the bootstrap css styles
<form method="post" id="form">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" name="name" id="name" class="form-control" value="" />
<p id="error_name"></p>
</div>
...
</form>
For the server side is the server() method
use Hispanic\Validation;
$validation = new Validation();
//Is possible change the labels attributes with the attribute() method
$attributes = array(
"name" => "Name",
"confirm_password" => "Confirm Password",
/* ... */
);
$validation->attributes($attributes);
$rules = array(
"name" => "required|regex:/^[a-z]+$/|min_length:3|max_length:50",
/* ... */
);
/* if you like the client validation is easy */
$validation->client($rules);
/* request */
if (isset($_POST["name"])) {
$validation->server($rules); //The second argument is optional for the custom messages array
//If is valid
if ($validation->isValid()) {
/* ok */
} else {
/* error */
//get associative array with all errors
$errors = $validation->getErrors();
//get only one error, the first error.
$first_error = $validation->getFirstError();
}
}
Validation rules options
- required : the field is required
- checked : the field needs to be checked
- min_length : minimum length of characters in string. Example: min_length:3
- max_length : maximum length of characters in string. Example: max_length:30
- min : minimum numeric value. Example: min:1
- max : maximum numeric value: Example: max:10
- between : range of characters allowed. Example: between:3-20
- range : range of allowable numeric values. Example: range:1-10
- name : only allowed a-záéíóúàèìòùäëïöüâêîôûñ\s. (ignored uppercase)
- alpha : only allowed a-záéíóúàèìòùäëïöüâêîôûñ (ignored uppercase)
- alphanumeric : only allowed 0-9a-záéíóúàèìòùäëïöüâêîôûñ (ignored uppercase)
- digit : only digits
- email : only a valid email
- ip : only a valid ip
- url : only a valid url
- date : only a valid date format. Example: date:Y-m-d
- time : only a valid time format. Example: time:H:i:s
- datetime : only a valid datetime format. Example: datetime:Y-m-d H:i:s
- regex : regular expression filter. Example: regex:/^[a-z]$/i
- equalsTo : The field value is equal to other field. Example: equalsTo:password
- float : only a float value
- integer : only a integer value
- numeric : only a numeric value
- contains : the field needs to contain one of the required values. Example: contains:one,two,three
- file_required : the input file is required
- min_files : minimum number of files allowed in input file multiple. Example: min_files:2
- max_files : maximum number of files allowed in input file multiple. Example: max_files:10
- file_min_size : minimum size allowed for file. Example 1MB: file_min_size:1048576
- file_max_size : maximum size allowed for file. Example 1024 bytes: file_min_size:1024
- mime : mime type allowed for file. Example: mime:pdf,txt,js
- img_min_width : minimum width allowed for image file. Example 250px: img_min_width:250
- img_max_width : maximum width allowed for image file. Example 1024px: img_max_width:1024
- img_min_height : minimum height allowed for image file. Example 250px: img_min_height:250
- img_max_height : maximum height allowed for image file. Example 1024px: img_max_height:1024