|
1
|
|
|
<?php defined('SYSPATH') OR die('No direct script access.'); |
|
2
|
|
|
/** |
|
3
|
|
|
* Jam Validatior Rule |
|
4
|
|
|
* |
|
5
|
|
|
* @package Jam |
|
6
|
|
|
* @category Validation |
|
7
|
|
|
* @author Ivan Kerin |
|
8
|
|
|
* @copyright (c) 2011-2012 Despark Ltd. |
|
9
|
|
|
* @license http://www.opensource.org/licenses/isc-license.txt |
|
10
|
|
|
*/ |
|
11
|
|
|
class Kohana_Jam_Validator_Rule_Numeric extends Jam_Validator_Rule { |
|
12
|
|
|
|
|
13
|
|
|
public $greater_than_or_equal_to; |
|
14
|
|
|
|
|
15
|
|
|
public $greater_than; |
|
16
|
|
|
|
|
17
|
|
|
public $equal_to; |
|
18
|
|
|
|
|
19
|
|
|
public $less_than; |
|
20
|
|
|
|
|
21
|
|
|
public $less_than_or_equal_to; |
|
22
|
|
|
|
|
23
|
|
|
public $between; |
|
24
|
|
|
|
|
25
|
|
|
public $odd; |
|
26
|
|
|
|
|
27
|
|
|
public $even; |
|
28
|
|
|
|
|
29
|
|
|
public $only_integer; |
|
30
|
|
|
|
|
31
|
30 |
|
public function validate(Jam_Validated $model, $attribute, $value) |
|
32
|
|
|
{ |
|
33
|
30 |
|
if ( ! is_numeric($value)) |
|
34
|
|
|
{ |
|
35
|
2 |
|
$model->errors()->add($attribute, 'numeric'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
30 |
|
if ($this->only_integer !== NULL AND ! (filter_var($value, FILTER_VALIDATE_INT) !== FALSE)) |
|
39
|
|
|
{ |
|
40
|
2 |
|
$model->errors()->add($attribute, 'numeric_only_integer'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
30 |
|
if ($this->greater_than_or_equal_to !== NULL AND ! ($value >= $this->greater_than_or_equal_to)) |
|
44
|
|
|
{ |
|
45
|
1 |
|
$model->errors()->add($attribute, 'numeric_greater_than_or_equal_to', array(':greater_than_or_equal_to' => $this->greater_than_or_equal_to)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
30 |
|
if ($this->greater_than !== NULL AND ! ($value > $this->greater_than)) |
|
49
|
|
|
{ |
|
50
|
2 |
|
$model->errors()->add($attribute, 'numeric_greater_than', array(':greater_than' => $this->greater_than)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
30 |
|
if ($this->equal_to !== NULL AND ! ($value == $this->equal_to)) |
|
54
|
|
|
{ |
|
55
|
2 |
|
$model->errors()->add($attribute, 'numeric_equal_to', array(':equal_to' => $this->equal_to)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
30 |
|
if ($this->less_than !== NULL AND ! ($value < $this->less_than)) |
|
59
|
|
|
{ |
|
60
|
2 |
|
$model->errors()->add($attribute, 'numeric_less_than', array(':less_than' => $this->less_than)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
30 |
|
if ($this->less_than_or_equal_to !== NULL AND ! ($value <= $this->less_than_or_equal_to)) |
|
64
|
|
|
{ |
|
65
|
1 |
|
$model->errors()->add($attribute, 'numeric_less_than_or_equal_to', array(':less_than_or_equal_to' => $this->less_than_or_equal_to)); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
30 |
|
if ($this->between !== NULL AND ! ($value >= $this->between[0] AND $value <= $this->between[1])) |
|
69
|
|
|
{ |
|
70
|
|
|
$model->errors()->add($attribute, 'numeric_between', array(':minimum' => $this->between[0], ':maximum' => $this->between[1])); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
30 |
|
if ($this->odd === TRUE AND ! ($value % 2 == 0)) |
|
74
|
|
|
{ |
|
75
|
2 |
|
$model->errors()->add($attribute, 'numeric_odd', array(':odd' => $this->odd)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
30 |
|
if ($this->even === TRUE AND ! ($value % 2 == 1)) |
|
79
|
|
|
{ |
|
80
|
2 |
|
$model->errors()->add($attribute, 'numeric_even', array(':even' => $this->even)); |
|
81
|
|
|
} |
|
82
|
30 |
|
} |
|
83
|
|
|
|
|
84
|
30 |
|
public function html5_validation() |
|
85
|
|
|
{ |
|
86
|
30 |
|
if ($this->only_integer) |
|
87
|
|
|
return array( |
|
88
|
6 |
|
'pattern' => '-?\d+', |
|
89
|
|
|
'title' => 'Integer numbers' |
|
90
|
|
|
); |
|
91
|
|
|
|
|
92
|
|
|
return array( |
|
93
|
24 |
|
'pattern' => '-?\d+(\.\d+)?', |
|
94
|
|
|
'title' => 'Numbers with an optional floating point' |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|