Validators

Note

The Validators module uses the static class ValidatorsManager to manage validation.

Validators are used to check that the member datas of an object complies with certain constraints.

Adding validators

Either the Author class that we want to use in our application :

app/models/Author.php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
     namespace models;

     class Author {
             /**
              * @var string
              * @validator("notEmpty")
              */
             private $name;

             public function getName(){
                     return $this->name;
             }

             public function setName($name){
                     $this->name=$name;
             }
     }

We added a validation constraint on the name member with the @validator annotation, so that it is not empty.

Generating cache

Run this command in console mode to create the cache data of the Author class :

Ubiquity init-cache -t=models

Validator cache is generated in app/cache/contents/validators/models/Author.cache.php.

Validating instances

an instance

public function testValidateAuthor(){
        $author=new Author();
        //Do something with $author
        $violations=ValidatorsManager::validate($author);
        if(sizeof($violations)>0){
                echo implode('<br>', ValidatorsManager::validate($author));
        }else{
                echo 'The author is valid!';
        }
}

if the name of the author is empty, this action should display:

name : This value should not be empty

The validate method returns an array of ConstraintViolation instances.

multiple instances

public function testValidateAuthors(){
        $authors=DAO::getAll(Author::class);
        $violations=ValidatorsManager::validateInstances($author);
        foreach($violations as $violation){
                echo $violation.'<br>';
        }
}

Models generation with default validators

When classes are automatically generated from the database, default validators are associated with members, based on the fields’ metadatas.

Ubiquity create-model User
app/models/Author.php
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
     namespace models;
     class User{
             /**
              * @id
              * @column("name"=>"id","nullable"=>false,"dbType"=>"int(11)")
              * @validator("id","constraints"=>array("autoinc"=>true))
             **/
             private $id;

             /**
              * @column("name"=>"firstname","nullable"=>false,"dbType"=>"varchar(65)")
              * @validator("length","constraints"=>array("max"=>65,"notNull"=>true))
             **/
             private $firstname;

             /**
              * @column("name"=>"lastname","nullable"=>false,"dbType"=>"varchar(65)")
              * @validator("length","constraints"=>array("max"=>65,"notNull"=>true))
             **/
             private $lastname;

             /**
              * @column("name"=>"email","nullable"=>false,"dbType"=>"varchar(255)")
              * @validator("email","constraints"=>array("notNull"=>true))
              * @validator("length","constraints"=>array("max"=>255))
             **/
             private $email;

             /**
              * @column("name"=>"password","nullable"=>true,"dbType"=>"varchar(255)")
              * @validator("length","constraints"=>array("max"=>255))
             **/
             private $password;

             /**
              * @column("name"=>"suspended","nullable"=>true,"dbType"=>"tinyint(1)")
              * @validator("isBool")
             **/
             private $suspended;
     }

These validators can then be modified.
Modifications must always be folowed by a re-initialization of the model cache.

Ubiquity init-cache -t=models

Models validation informations can be displayed with devtools :

Ubiquity info:validation -m=User
../_images/info-validation-devtools.png

Gets validators on email field:

Ubiquity info:validation email -m=User
../_images/info-validation-email-devtools.png

Validation informations are also accessible from the models part of the webtools:

../_images/info-validation-webtools.png

Validator types

Basic

Validator Roles Constraints Accepted values
isBool Check if value is a boolean   true,false,0,1
isEmpty Check if value is empty   ‘’,null
isFalse Check if value is false   false,’false’,0,’0’
isNull Check if value is null   null
isTrue Check if value is true   true,’true’,1,’1’
notEmpty Check if value is not empty   !null && !’’
notNull Check if value is not null   !null
type Check if value is of type {type} {type}  

Comparison

Dates

Multiples

Strings