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_Length extends Jam_Validator_Rule { |
12
|
|
|
|
13
|
|
|
public $minimum; |
14
|
|
|
|
15
|
|
|
public $maximum; |
16
|
|
|
|
17
|
|
|
public $between; |
18
|
|
|
|
19
|
|
|
public $is; |
20
|
|
|
|
21
|
25 |
|
public function validate(Jam_Validated $model, $attribute, $value) |
22
|
|
|
{ |
23
|
25 |
|
$length = mb_strlen($value); |
24
|
|
|
|
25
|
25 |
|
if ($this->minimum !== NULL AND ! ($length >= $this->minimum)) |
26
|
|
|
{ |
27
|
5 |
|
$model->errors()->add($attribute, 'length_minimum', array(':minimum' => $this->minimum)); |
28
|
|
|
} |
29
|
|
|
|
30
|
25 |
|
if ($this->maximum !== NULL AND ! ($length <= $this->maximum)) |
31
|
|
|
{ |
32
|
4 |
|
$model->errors()->add($attribute, 'length_maximum', array(':maximum' => $this->maximum)); |
33
|
|
|
} |
34
|
|
|
|
35
|
25 |
|
if ($this->between !== NULL AND ! ($length >= $this->between[0] AND $length <= $this->between[1])) |
36
|
|
|
{ |
37
|
4 |
|
$model->errors()->add($attribute, 'length_between', array(':minimum' => $this->between[0], ':maximum' => $this->between[1])); |
38
|
|
|
} |
39
|
|
|
|
40
|
25 |
|
if ($this->is !== NULL AND ! ($length == $this->is)) |
41
|
|
|
{ |
42
|
3 |
|
$model->errors()->add($attribute, 'length_is', array('is' => $this->is)); |
43
|
|
|
} |
44
|
25 |
|
} |
45
|
|
|
|
46
|
22 |
|
public function html5_validation() |
47
|
|
|
{ |
48
|
22 |
|
if ($this->is) |
49
|
|
|
{ |
50
|
|
|
return array( |
51
|
4 |
|
'pattern' => ".{{$this->is}}", |
52
|
4 |
|
'title' => "Value must be $this->is letters" |
53
|
|
|
); |
54
|
|
|
} |
55
|
18 |
|
elseif ($this->between) |
56
|
|
|
{ |
57
|
|
|
return array( |
58
|
6 |
|
'pattern' => ".{{$this->between[0]},{$this->between[1]}}", |
59
|
6 |
|
'title' => "Value must be longer than {$this->between[0]} and shorter than {$this->between[1]} letters" |
60
|
|
|
); |
61
|
|
|
} |
62
|
12 |
|
elseif ($this->minimum OR $this->maximum) |
63
|
|
|
{ |
64
|
12 |
|
$minimum = $this->minimum ? $this->minimum : 0; |
65
|
|
|
return array( |
66
|
12 |
|
'pattern' => ".{{$minimum},{$this->maximum}}", |
67
|
|
|
'title' => 'Value must be ' |
68
|
12 |
|
.($this->minimum ? "longer than $this->minimum".($this->maximum ? ' and ' : '') : '') |
69
|
12 |
|
.($this->maximum ? "shorter than $this->maximum" : '').' letters' |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|