DataValidator: contains() method

In the last post I presented to you a class for data validation that I implemented and made available through my link on GitHub for the whole php community to be able to use, guess and why not contribute.

Well then. From this post and in future posts I will address some of the validation methods that I think are important and that it is interesting to emphasize the techniques for a better use of them.Today I’m going to comment on contains() and not_contains() methods.The contains() method is intended to allow only certain values ​​within a field. For example, imagine that you want to validate a “Gender” field and that field should only allow the “M” or “F” values . If something other than these two characters is passed, the field will not be validated. You can set the values ​​for the method in an array  or in a string where each value must be separated by some character of your choice (comma, dash, underline, pipe, etc.). Look:

1
2
3
4
5
6
7
8
9
10
11
12
ini_set('default_charset','UTF-8');    
include 'DataValidator.php';
 
$validate = new Data_Validator();
 
// 1st - Using array
$allowed_values = array('M', 'F'); //Male or Female
$validate->set('gender', 'M')->contains($allowed_values);
 
// 2nd - Using String with separators
$allowed_values = 'M, F';
$validate->set('gender', 'M')->contains($allowed_values, ','); //Note that in the second parameter I must inform which character identifies the separation of the values

In turn, the not_contains () method simply performs the reverse process. It checks for values ​​that cannot be contained in the field.

1
2
3
4
5
6
7
8
9
10
11
12
ini_set('default_charset','UTF-8');    
include 'DataValidator.php';
 
$validate = new Data_Validator();
 
// 1st - Using array
$not_alloweds = array('admin', 'adm', 'administrador'); 
$validate->set('user', 'admin')->not_contains($not_alloweds);
 
// 2nd - Using String with separators
$not_alloweds = 'admin-adm-administrador';
$validate->set('user', 'admin')->not_contains($not_alloweds, '-'); //Note that in the second parameter I must inform which character identifies the separation of the values

That’s it for today. Remembering that any contribution, criticism and suggestion is welcome.

Hugs!!!

Holds a university degree in Information Systems, a postgraduate degree in Database Systems and a master's degree in Education with a focus on Sociocommunity Technologies. He works as a professor of technical and technological education at the Federal Institute of Education, Science and Technology of São Paulo, teaching subjects in the areas of programming, database, project development and software engineering.

Posts relacionados

Leave a Reply

Your email address will not be published.