
Valid data validation
The problem of data validation was repeatedly raised on Habré. Server-side validation and client-side validation were discussed. We described the use of various frameworks for this purpose such as Yii, CodeIgniter, Vanadium, .NET and JQuery plugins.
But there was no comprehensive view of these problems, as a logically unified task for both the server and the client.
Validation of data should be transparent no matter where it takes place - on the client or on the server, you do not need to break it into two separate parts and make a separate implementation for each of the tasks. After several projects and doing the same work, the understanding comes that it would be nice to combine these parts together and not waste your precious time on the actual duplication of code.
So, the question is posed as the creation of a transparent data validation system without a logical separation between the client and server. Server-side validation should automatically provide client-side validation without additional effort on the part of the programmer.
To solve this problem, we will use the existing data validation modules in PHP and Javascript. The first framework will be responsible for server-side validation, and the second framework will validate data on the client side. They will be our workhorses and we can only find a way to connect these frameworks with each other.
We solve the problem trivially - we create a class for declaring data verification rules. The meaning of this is that having a set of some rules we can apply them both on the server side and on the client side. There is a specificity, but it comes down to correctly interpreting these rules into commands of a specific validation framework.
Now the validation will take place according to the following scheme:
1. We define the rules
2. We pass the rules to the PHP wrapper class for application on the server side.
3. Convert the rules into a JavaScript array and pass it to the client, where at the right time the data will be checked by JavaScript with a wrapper class on the client side.
Example: You can find a working example here , and
source codes of the example are here . Validation works on the client side with Javascript enabled, otherwise validation is performed only on the server side.
But there was no comprehensive view of these problems, as a logically unified task for both the server and the client.
Validation of data should be transparent no matter where it takes place - on the client or on the server, you do not need to break it into two separate parts and make a separate implementation for each of the tasks. After several projects and doing the same work, the understanding comes that it would be nice to combine these parts together and not waste your precious time on the actual duplication of code.
So, the question is posed as the creation of a transparent data validation system without a logical separation between the client and server. Server-side validation should automatically provide client-side validation without additional effort on the part of the programmer.
To solve this problem, we will use the existing data validation modules in PHP and Javascript. The first framework will be responsible for server-side validation, and the second framework will validate data on the client side. They will be our workhorses and we can only find a way to connect these frameworks with each other.
We solve the problem trivially - we create a class for declaring data verification rules. The meaning of this is that having a set of some rules we can apply them both on the server side and on the client side. There is a specificity, but it comes down to correctly interpreting these rules into commands of a specific validation framework.
Now the validation will take place according to the following scheme:
1. We define the rules
2. We pass the rules to the PHP wrapper class for application on the server side.
3. Convert the rules into a JavaScript array and pass it to the client, where at the right time the data will be checked by JavaScript with a wrapper class on the client side.
Example: You can find a working example here , and
$validate=new Validate();
$validate//определение правил
->ruleUrl('url','введите правильно ссылку')
->ruleEmail('email','введите правильно адрес электронной почты',0);
if($_POST)
$validate->check($_POST,$errors);//проверка массива $_POST, ошибки помещаются в массив $errors
$js_main=$validate->js('main');//преобразуем правила в объект JavaScript
…..
function validate()//функция на JavaScript для валидации данных
{
return main.checkForm($('#form'));//данные берутся из формы по именам
}
source codes of the example are here . Validation works on the client side with Javascript enabled, otherwise validation is performed only on the server side.