Validation module has just been massively updated. I’ve made a move from the array based definition to a somewhat more verbose OOP style. You will also find some tasty new features like step-by-step validation, throwing exceptions and redone conditional rules. You can read the details in the documentation, but here I will show you a quick demo of what it is capable of (and yes I believe this is the most flexible validation system I’ve ever seen)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//Initialize validator
$validate = $pixie->validate->get($some_data);
/* Basic stuff */
$validate->field('username')
//Adding multiple rules
->rules('filled','alpha_numeric')
//Adding a rule that requires extra parameters
->rule('min_length',8)
//Setting a custom error message
->error('Username is invalid');
//Adding password and password confirmation
$validate->field('password')
->rule('filled');
$validate->field('password_confirm')
->rule('same_as', 'password');
/* Conditional rules */
//The sate field is required...
$validate->field('state')
->rule('filled')
->rule('in', $us_states)
//...only if the 'country' field is 'US'
->condition('country')
->rule('filled')
->rule('equals', 'US');
//'city' is required if the country is other than US
$validate->field('city')
->rule('filled')
->condition('country')
->rule('filled')
//Notice how we can inverse the rule with '!'
->rule('!equals', 'US');
/**
* Some rules should throw exceptions, instead of attacker-friendly error messages
*/
$validate->field('secret_token')
->rule('filled')
//Yes, it supports callbacks
->rule('callback',function($token){
//validate token, return true if valid
})
->throw_on_error("Someone is trying to hack us");
/**
* Step by step validation
* E.g. validate some rules only if previous ones are valid
*/
$validate->field('username')
->rule('filled','alpha_numeric');
//The second 'true' parameter means those rules will be applied if the
//previous ones were valid.
//In our case there is no need to check invalid usernames for uniqueness
$validate->field('username',true)
->rule('callback',function(){
//Check if username is unique
});
/* Combine with use() statement for even more greatness */
$parent = null;
$validate->field('parent_id')
->rule('callback', function($parent_id) use ($parent){
$parent = ... //Get the parent model
return $parent->loaded();
})
->error("Parent doesn't exist");
//Since we passed parent in a use() statement in previous callback
//It will be already present in the second one.
//Furthemore the following rule will run only if the first one was valid
$validate->field('parent_id', true)
->rule('callback', function($parent_id) use ($parent){
//Check if $parent can be a parent for the current item
})
->error('The parent specified is invalid');
/* Finally get results */
//check if valid
$validate->valid();
//get errors
$validate->errors();