00001 <?php
00010 class valid extends object {
00011
00017 protected $errors = array();
00018
00024 public function getValue() {
00025 return $this->cfg->value;
00026 }
00027
00033 public function setValue(&$value) {
00034 $this->cfg->set('value', $value);
00035 }
00036
00044 public function addRule($type, $prm=null, $msg=null) {
00045 if (!is_array($prm) && !is_callable($prm))
00046 $prm = array($prm);
00047 $this->cfg->setInArray('rules', $type, $prm);
00048 if (!is_null($msg))
00049 $this->setMessage($type, $msg);
00050 }
00051
00058 public function hasRule($type) {
00059 return $this->cfg->checkInArray('rules', $type);
00060 }
00061
00067 public function delRule($type) {
00068 $this->cfg->delInArray('rules', $type);
00069 }
00070
00076 public function getRules() {
00077 return $this->cfg->rules;
00078 }
00079
00085 public function isValid() {
00086 $this->errors = array();
00087 $valid = true;
00088 $noNeedRequired = $this->cfg->noNeedRequired;
00089 $val = $this->cfg->validEltArray && is_array($this->cfg->value) ? $this->cfg->value : array($this->cfg->value);
00090 foreach($this->cfg->rules as $rule=>$prm) {
00091 if (!is_numeric($rule)) {
00092 foreach($val as $v) {
00093 if ((in_array($rule, $noNeedRequired)) || !empty($v))
00094 $valid = $this->{'is'.ucfirst($rule)}($v, $prm) && $valid;
00095 }
00096 }
00097 }
00098 return $valid;
00099 }
00100
00108 public function isRequired($val, $prm=null) {
00109 if (empty($val)) {
00110 $this->errors[] = sprintf($this->getMessage('required'), $this->cfg->label);
00111 return false;
00112 }
00113 return true;
00114 }
00115
00123 public function isNumeric($val, $prm=null) {
00124 if (!is_numeric($val)) {
00125 $this->errors[] = sprintf($this->getMessage('numeric'), $this->cfg->label);
00126 return false;
00127 }
00128 return true;
00129 }
00130
00138 public function isInt($val, $prm=null) {
00139 if (!is_numeric($val) || $val != (int)$val) {
00140 $this->errors[] = sprintf($this->getMessage('int'), $this->cfg->label);
00141 return false;
00142 }
00143 return true;
00144 }
00145
00153 public function isDifferent($val, $prm=null) {
00154 if ($val == $prm[0]) {
00155 $this->errors[] = sprintf($this->getMessage('different'), $this->cfg->label, $prm[0]);
00156 return false;
00157 }
00158 return true;
00159 }
00160
00168 public function isIn($val, $prm=null) {
00169 $ret = true;
00170 $val = is_array($val)? $val : array($val);
00171 $val = array_filter($val);
00172 if (!empty($val))
00173 foreach($val as $v) {
00174 if (!in_array($v, $prm)) {
00175 $this->errors[] = sprintf($this->getMessage('in'), $v, $this->cfg->label);
00176 $ret = false;
00177 }
00178 }
00179 return $ret;
00180 }
00181
00189 public function isEqual($val, $prm=null) {
00190 $ret = true;
00191 if ($prm[0] instanceof form_abstract) {
00192 if ($val != $prm[0]->getRawValue()) {
00193 $this->errors[] = sprintf($this->getMessage('equalInput'), $this->cfg->label, $prm[0]->label);
00194 $ret = false;
00195 }
00196 } else {
00197 if ($val != $prm[0]) {
00198 $this->errors[] = sprintf($this->getMessage('equal'), $v, $this->cfg->label);
00199 $ret = false;
00200 }
00201 }
00202 return $ret;
00203 }
00204
00214 public function isGroupedFields($val, $prm=null) {
00215 $nbFilled = 0;
00216 foreach($prm['fields'] as $f) {
00217 $val = $prm['form']->getValue($f);
00218 if (!empty($val))
00219 $nbFilled++;
00220 }
00221 $ret = $nbFilled == 0 || $nbFilled == count($prm['fields']);
00222 if (!$ret) {
00223
00224 $this->errors[] = sprintf($this->getMessage('groupedFields'), $this->cfg->label);
00225 return false;
00226 }
00227 return true;
00228 }
00229
00239 public function isAtLeastOneField($val, $prm=null) {
00240 $nbFilled = 0;
00241 foreach($prm['fields'] as $f) {
00242 $val = $prm['form']->getValue($f);
00243 if (!empty($val))
00244 $nbFilled++;
00245 }
00246 if ($nbFilled == 0) {
00247
00248 $this->errors[] = sprintf($this->getMessage('atLeastOneField'), $this->cfg->label);
00249 return false;
00250 }
00251 return true;
00252 }
00253
00261 public function isCallback($val, $prm=null) {
00262 $tmp = call_user_func($prm, $val);
00263 if ($tmp !== true) {
00264 $tmp = is_string($tmp) ? $tmp : 'callback';
00265 $msg = $this->getMessage($tmp);
00266 $this->errors[] = $msg ? sprintf($msg, $this->cfg->label) : $tmp;
00267 return false;
00268 }
00269 return true;
00270 }
00271
00279 public function isUrl($val, $prm=null) {
00280 if (!filter_var($val, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED)) {
00281 $this->errors[] = sprintf($this->getMessage('url'), $this->cfg->label);
00282 return false;
00283 }
00284 return true;
00285 }
00286
00294 public function isEmail($val, $prm=null) {
00295 if (!filter_var($val, FILTER_VALIDATE_EMAIL)) {
00296 $this->errors[] = sprintf($this->getMessage('email'), $this->cfg->label);
00297 return false;
00298 }
00299 return true;
00300 }
00301
00312 public function isDbUnique($val, array $prm) {
00313 if (array_key_exists('value', $prm) && $val == $prm['value'])
00314 return true;
00315
00316 $table = $prm['table'] instanceof db_table? $prm['table'] : db::get('table', $prm['table']);
00317 $nb = $table->count(array(
00318 'where'=>array($prm['field']=>$val),
00319 'whereOp'=>'LIKE'
00320 ));
00321 if ($nb > 0) {
00322 $this->errors[] = sprintf($this->getMessage('dbUnique'), $val, $this->cfg->label);
00323 return false;
00324 }
00325 return true;
00326 }
00327
00337 public function isDbExists($val, array $prm) {
00338 $table = $prm['table'] instanceof db_table? $prm['table'] : db::get('table', $prm['table']);
00339 $nb = $table->count(array(
00340 'where'=>array($prm['field']=>$val),
00341 'whereOp'=>'LIKE'
00342 ));
00343 if ($nb == 0) {
00344 $this->errors[] = sprintf($this->getMessage('dbExists'), $val, $this->cfg->label);
00345 return false;
00346 }
00347 return true;
00348 }
00349
00356 protected function getMessage($name) {
00357 return utils::htmlOut($this->cfg->getInArray('messages', $name));
00358 }
00359
00366 public function setMessage($name, $msg) {
00367 $this->cfg->setInArray('messages', $name, $msg);
00368 }
00369
00375 public function getErrors() {
00376 return $this->errors;
00377 }
00378 }