My favorites | Sign in
yii
Project Home Downloads Issues Source
Checkout   Browse   Changes    
 
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
<?php
/**
* CTypeValidator class file.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2011 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/

/**
* CTypeValidator verifies if the attribute is of the type specified by {@link type}.
*
* The following data types are supported:
* <ul>
* <li><b>integer</b> A 32-bit signed integer data type.</li>
* <li><b>float</b> A double-precision floating point number data type.</li>
* <li><b>string</b> A string data type.</li>
* <li><b>array</b> An array value. </li>
* <li><b>date</b> A date data type.</li>
* <li><b>time</b> A time data type.</li>
* <li><b>datetime</b> A date and time data type.</li>
* </ul>
*
* For <b>date</b> type, the property {@link dateFormat}
* will be used to determine how to parse the date string. If the given date
* value doesn't follow the format, the attribute is considered as invalid.
*
* Starting from version 1.1.7, we have a dedicated date validator {@link CDateValidator}.
* Please consider using this validator to validate a date-typed value.
*
* When using the {@link message} property to define a custom error message, the message
* may contain additional placeholders that will be replaced with the actual content. In addition
* to the "{attribute}" placeholder, recognized by all validators (see {@link CValidator}),
* CTypeValidator allows for the following placeholders to be specified:
* <ul>
* <li>{type}: replaced with data type the attribute should be {@link type}.</li>
* </ul>
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.validators
* @since 1.0
*/
class CTypeValidator extends CValidator
{
/**
* @var string the data type that the attribute should be. Defaults to 'string'.
* Valid values include 'string', 'integer', 'float', 'array', 'date', 'time' and 'datetime'.
*/
public $type='string';
/**
* @var string the format pattern that the date value should follow. Defaults to 'MM/dd/yyyy'.
* Please see {@link CDateTimeParser} for details about how to specify a date format.
* This property is effective only when {@link type} is 'date'.
*/
public $dateFormat='MM/dd/yyyy';
/**
* @var string the format pattern that the time value should follow. Defaults to 'hh:mm'.
* Please see {@link CDateTimeParser} for details about how to specify a time format.
* This property is effective only when {@link type} is 'time'.
*/
public $timeFormat='hh:mm';
/**
* @var string the format pattern that the datetime value should follow. Defaults to 'MM/dd/yyyy hh:mm'.
* Please see {@link CDateTimeParser} for details about how to specify a datetime format.
* This property is effective only when {@link type} is 'datetime'.
*/
public $datetimeFormat='MM/dd/yyyy hh:mm';
/**
* @var boolean whether the attribute value can be null or empty. Defaults to true,
* meaning that if the attribute is empty, it is considered valid.
*/
public $allowEmpty=true;

/**
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
* @param CModel $object the object being validated
* @param string $attribute the attribute being validated
*/
protected function validateAttribute($object,$attribute)
{
$value=$object->$attribute;
if($this->allowEmpty && $this->isEmpty($value))
return;

if($this->type==='integer')
$valid=preg_match('/^[-+]?[0-9]+$/',trim($value));
else if($this->type==='float')
$valid=preg_match('/^[-+]?([0-9]*\.)?[0-9]+([eE][-+]?[0-9]+)?$/',trim($value));
else if($this->type==='date')
$valid=CDateTimeParser::parse($value,$this->dateFormat,array('month'=>1,'day'=>1,'hour'=>0,'minute'=>0,'second'=>0))!==false;
else if($this->type==='time')
$valid=CDateTimeParser::parse($value,$this->timeFormat)!==false;
else if($this->type==='datetime')
$valid=CDateTimeParser::parse($value,$this->datetimeFormat, array('month'=>1,'day'=>1,'hour'=>0,'minute'=>0,'second'=>0))!==false;
else if($this->type==='array')
$valid=is_array($value);
else
return;

if(!$valid)
{
$message=$this->message!==null?$this->message : Yii::t('yii','{attribute} must be {type}.');
$this->addError($object,$attribute,$message,array('{type}'=>$this->type));
}
}
}

Change log

r3527 by qiang.xue on Dec 31, 2011   Diff
1.1.9 release.
Go to: 
Sign in to write a code review

Older revisions

r3515 by mdomba on Dec 28, 2011   Diff
removed 1.0.x reference from
documentation
r3491 by jefftulsa on Dec 16, 2011   Diff
adding documentation about
placeholders in different validator
custom error messages - addressing
 Issue 2152 
r3052 by qiang.xue on Mar 12, 2011   Diff
* (Fixes  issue 2069 ) Added
CDateValidator
All revisions of this file

File info

Size: 4342 bytes, 110 lines

File properties

svn:keywords
Id
Powered by Google Project Hosting