Пример #1
0
 public function testMostUsedWay()
 {
     $username = '******';
     $password = '******';
     $dtds['username'] = new LtValidatorDtd($_POST['username'], "username", array("max_length" => 8, "mask" => "/^[a-z0-9]+\$/i", "ban" => "/f**k/"), array("mask" => "用户名只能由数字或字组成", "ban" => "用户名不能包含脏话"));
     $dtds['password'] = new LtValidatorDtd($_POST['password'], "password", array("max_length" => 8, "mask" => "/^[a-z0-9]+\$/i", "ban" => "/f**k/"), array("max_length" => "密码最长8位", "mask" => "密码只能由数字或字组成"));
     // 配置文件
     $config['validator.error_messages'] = array('ban' => '%s contain banned words', 'mask' => '%s does not match the given format', 'max_length' => '%s is longer than %s', 'min_length' => '%s is shorter than %s', 'max_value' => '%s is bigger than %s', 'min_value' => '%s is smaller than %s', 'max_selected' => '%s is too much', 'min_selected' => '%s is too few', 'required' => '%s is empty', 'equal_to' => '%s is not equal to %s');
     $configHandle = new LtConfig();
     $configHandle->addConfig($config);
     $validator = new LtValidator();
     $validator->configHandle = $configHandle;
     $validator->init();
     $dtd = $dtds['username'];
     foreach ($dtd->rules as $ruleKey => $ruleValue) {
         if ($ruleValue instanceof LtConfigExpression) {
             eval('$_ruleValue = ' . $ruleValue->__toString());
             $dtd->rules[$ruleKey] = $ruleValue;
         }
     }
     $error_messages = $validator->validate($dtd);
     $this->assertEquals(array('max_length' => 'username is longer than 8', 'mask' => '用户名只能由数字或字组成', 'ban' => '用户名不能包含脏话'), $error_messages);
     $dtd = $dtds['password'];
     foreach ($dtd->rules as $ruleKey => $ruleValue) {
         if ($ruleValue instanceof LtConfigExpression) {
             eval('$_ruleValue = ' . $ruleValue->__toString());
             $dtd->rules[$ruleKey] = $ruleValue;
         }
     }
     $error_messages = $validator->validate($dtd);
     $this->assertEquals(array('max_length' => '密码最长8位', 'mask' => '密码只能由数字或字组成', 'ban' => 'password contain banned words'), $error_messages);
 }
Пример #2
0
 /**
  * Validate the data from client
  * 
  * @return array
  * @todo 从config中取dtd配置
  */
 protected function validateInput()
 {
     $validateResult = array("error_total" => 0, "error_messages" => array());
     if (!empty($this->dtds) && class_exists('LtValidator')) {
         $validator = new LtValidator();
         $validator->init();
         foreach ($this->dtds as $variable => $dtd) {
             $from = isset($dtd->from) ? $dtd->from : 'request';
             foreach ($dtd->rules as $ruleKey => $ruleValue) {
                 if ($ruleValue instanceof LtConfigExpression) {
                     $_ruleValue = NULL;
                     eval('$_ruleValue = ' . $ruleValue->__toString());
                     $dtd->rules[$ruleKey] = $_ruleValue;
                 }
             }
             $error_messages = $validator->validate($this->context->{$from}($variable), $dtd);
             if (!empty($error_messages)) {
                 $validateResult['error_total']++;
                 $validateResult['error_messages'][$variable] = $error_messages;
             }
         }
     }
     return $validateResult;
 }