Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
FGValidator.php
1 <?php
3 {
4  /*public $validationTypes = array(
5  'number',
6  'telephone',
7  'socialsecurity',
8  'date',
9  'time',
10  'email'
11  );*/
12 
13  public function validate($validationType, $model, $name, $value)
14  {
15  $method = 'validate'.ucfirst($validationType);
16  if(method_exists($this, $method)) {
17  $this->$method($model, $name, $value);
18  }
19  }
20 
21  public function validateElement($elementType, $model, $name, $value)
22  {
23  $method = 'validateElement'.ucfirst($elementType);
24  if(method_exists($this, $method)) {
25  $this->$method($model, $name, $value);
26  }
27  }
28 
29  public function addError($object,$attribute,$message,$params=array())
30  {
31  $element = $object->getElementFromJSONByName($attribute);
32  $params['{attribute}'] = isset($element['label']) && $element['label'] !== ''? $element['label'] : $attribute;
33  $object->addError($attribute, strtr($message,$params));
34  }
35 
36  /* Element validators */
37  public function validateElementCaptcha($model, $name, $value)
38  {
39  $config = FgModule::getConfig();
40  #include dirname(__FILE__).'/../assets/elements/captcha/securimage/securimage.php';
41  #$securimage = new Securimage();
42  #if($securimage->check($value) == false) {
43  # $message = FgModule::t('captcha-invalid', 'validation');
44  # $this->addError($model,$name,$message);
45  #}
46  $code = Yii::app()->controller->createAction('captcha')->verifyCode;
47  if($value != $code) {
48  $message = FgModule::t('captcha-invalid', 'validation');
49  $this->addError($model,$name,$message);
50  }
51  }
52 
53  /* Validators */
54 
55  public function validateNumber($model, $name, $value)
56  {
57  $validator = new CNumberValidator();
58  if(!preg_match($validator->numberPattern, strval($value)))
59  {
60  $message = FgModule::t('number-invalid', 'validation');
61  $this->addError($model,$name,$message);
62  }
63  }
64 
65  public function validateTelephone($model, $name, $value)
66  {
67  $valid = true;
68  if(preg_match('/^[\s\d\+\/]*$/', $value) == false) {
69  $valid = false;
70  } else {
71  if(preg_match('/\d+/', $value) == false)
72  $valid = false;
73  }
74  if($valid == false) {
75  $message = FgModule::t('telephone-invalid', 'validation');
76  $this->addError($model,$name,$message);
77  }
78  }
79 
80  public function validateSocialsecurity($model, $name, $value)
81  {
82  $valid = true;
83  if(preg_match('/^\s*\d{10}\s*$/', $value) == false) {
84  $valid = false;
85  } else {
86  preg_match('/(\d{6})\s*$/', $value, $m);
87  $timestamp=CDateTimeParser::parse($m[1],'ddMMyy',array('month'=>1,'day'=>1,'hour'=>0,'minute'=>0,'second'=>0));
88  if($timestamp === false || $timestamp >= time()) {
89  $valid = false;
90  }
91  }
92  if($valid == false) {
93  $message = FgModule::t('socialsecurity-invalid', 'validation');
94  $this->addError($model,$name,$message);
95  }
96  }
97 
98  public function validateDate($model, $name, $value)
99  {
100  $validator = new CDateValidator();
101  $timestamp=CDateTimeParser::parse($value,'dd.MM.yyyy',array('month'=>1,'day'=>1,'hour'=>0,'minute'=>0,'second'=>0));
102  if($timestamp === false) {
103  $message = FgModule::t('date-invalid', 'validation');
104  $this->addError($model,$name,$message);
105  }
106  }
107 
108  public function validateTime($model, $name, $value)
109  {
110  $valid = true;
111  if(preg_match('/^\s*\d{2}:\d{2}\s*$/', $value) == false) {
112  $valid = false;
113  } else {
114  $time = explode(':', $value);
115  if(CTimestamp::isValidTime($time[0], $time[1], 0) == false)
116  $valid = false;
117  }
118  if($valid == false) {
119  $message = FgModule::t('time-invalid', 'validation');
120  $this->addError($model,$name,$message);
121  }
122  }
123 
124  public function validateEmail($model, $name, $value)
125  {
126  $validator = new CEmailValidator();
127  if($validator->validateValue($value) == false) {
128  $message = FgModule::t('email-invalid', 'validation');
129  $this->addError($model,$name,$message);
130  }
131  }
132 }