|
1
|
|
|
<?php |
|
2
|
|
|
namespace Ajax\semantic\components\validation; |
|
3
|
|
|
use Ajax\service\AjaxCall; |
|
4
|
|
|
use Ajax\JsUtils; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* @author jc |
|
8
|
|
|
* @version 1.001 |
|
9
|
|
|
* Generates a JSON Rule for the validation of a field |
|
10
|
|
|
*/ |
|
11
|
|
|
class Rule implements \JsonSerializable{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
private $type; |
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $prompt; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private $value; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct($type,$prompt=NULL,$value=NULL){ |
|
27
|
|
|
$this->type=$type; |
|
28
|
|
|
$this->prompt=$prompt; |
|
29
|
|
|
$this->value=$value; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function getType() { |
|
33
|
|
|
return $this->type; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function setType($type) { |
|
37
|
|
|
$this->type=$type; |
|
38
|
|
|
return $this; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function getPrompt() { |
|
42
|
|
|
return $this->prompt; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function setPrompt($prompt) { |
|
46
|
|
|
$this->prompt=$prompt; |
|
47
|
|
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function getValue() { |
|
51
|
|
|
return $this->value; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function setValue($value) { |
|
55
|
|
|
$this->value=$value; |
|
56
|
|
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function jsonSerialize() { |
|
60
|
|
|
$result= ["type"=>$this->type]; |
|
61
|
|
|
if(isset($this->prompt)) |
|
62
|
|
|
$result["prompt"]=$this->prompt; |
|
63
|
|
|
if(isset($this->value)) |
|
64
|
|
|
$result["value"]=$this->value; |
|
65
|
|
|
return $result; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* A field should match the value of another validation field, for example to confirm passwords |
|
70
|
|
|
* @param string $name |
|
71
|
|
|
* @param string $prompt |
|
72
|
|
|
* @return \Ajax\semantic\components\validation\Rule |
|
73
|
|
|
*/ |
|
74
|
|
|
public static function match($name,$prompt=NULL){ |
|
75
|
|
|
return new Rule("match[".$name."]",$prompt); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* A field should be different than another specified field |
|
80
|
|
|
* @param string $name |
|
81
|
|
|
* @param string $prompt |
|
82
|
|
|
* @return \Ajax\semantic\components\validation\Rule |
|
83
|
|
|
*/ |
|
84
|
|
|
public static function different($name,$prompt=NULL){ |
|
85
|
|
|
return new Rule("different[".$name."]",$prompt); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* A field is an integer value, or matches an integer range |
|
90
|
|
|
* @param int|NULL $min |
|
91
|
|
|
* @param int|NULL $max |
|
92
|
|
|
* @param string $prompt |
|
93
|
|
|
* @return \Ajax\semantic\components\validation\Rule |
|
94
|
|
|
*/ |
|
95
|
|
|
public static function integer($min=NULL,$max=NULL,$prompt=NULL){ |
|
96
|
|
|
if(\is_int($min) && \is_int($max)) |
|
97
|
|
|
return new Rule("integer[{$min}..{$max}]",$prompt); |
|
98
|
|
|
return new Rule("integer",$prompt); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public static function decimal($prompt=NULL){ |
|
102
|
|
|
return new Rule("decimal",$prompt); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public static function number($prompt=NULL){ |
|
106
|
|
|
return new Rule("number",$prompt); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public static function is($value,$prompt=NULL){ |
|
110
|
|
|
return new Rule("is[".$value."]",$prompt); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public static function isExactly($value,$prompt=NULL){ |
|
114
|
|
|
return new Rule("isExactly[".$value."]",$prompt); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
public static function not($value,$prompt=NULL){ |
|
118
|
|
|
return new Rule("not[".$value."]",$prompt); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public static function notExactly($value,$prompt=NULL){ |
|
122
|
|
|
return new Rule("notExactly[".$value."]",$prompt); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public static function contains($value,$prompt=NULL){ |
|
126
|
|
|
return new Rule("contains[".$value."]",$prompt); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public static function containsExactly($value,$prompt=NULL){ |
|
130
|
|
|
return new Rule("containsExactly[".$value."]",$prompt); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public static function doesntContain($value,$prompt=NULL){ |
|
134
|
|
|
return new Rule("doesntContain[".$value."]",$prompt); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public static function doesntContainExactly($value,$prompt=NULL){ |
|
138
|
|
|
return new Rule("doesntContainExactly[".$value."]",$prompt); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public static function minCount($value,$prompt=NULL){ |
|
142
|
|
|
return new Rule("minCount[".$value."]",$prompt); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public static function maxCount($value,$prompt=NULL){ |
|
146
|
|
|
return new Rule("maxCount[".$value."]",$prompt); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public static function exactCount($value,$prompt=NULL){ |
|
150
|
|
|
return new Rule("exactCount[".$value."]",$prompt); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public static function email($prompt=NULL){ |
|
154
|
|
|
return new Rule("email",$prompt); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public static function url($prompt=NULL){ |
|
158
|
|
|
return new Rule("url",$prompt); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public static function regExp($value,$prompt=NULL){ |
|
162
|
|
|
return new Rule("regExp",$prompt,$value); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public static function custom($name,$jsFunction){ |
|
166
|
|
|
return "$.fn.form.settings.rules.".$name." =".$jsFunction ; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
public static function ajax(JsUtils $js,$name,$url,$params,$jsCallback,$method="post",$parameters=[]){ |
|
170
|
|
|
$parameters=\array_merge(["async"=>false,"url"=>$url,"params"=>$params,"hasLoader"=>false,"jsCallback"=>$jsCallback,"dataType"=>"json","stopPropagation"=>false,"preventDefault"=>false,"responseElement"=>null],$parameters); |
|
171
|
|
|
$ajax=new AjaxCall($method, $parameters); |
|
172
|
|
|
return self::custom($name, "function(value){var result=true;".$ajax->compile($js)."console.log(result);return result;}"); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
|