/** * 对数据进行验证,返回所有未通过验证数据的错误信息 * * @param array $data 要验证的数据 * @param array|string $props 指定仅仅验证哪些属性 * @param string $mode 验证模式 * * @return array 所有没有通过验证的属性名称及验证规则 */ function validate(array $data, $props = null, $mode = 'general') { if (!is_null($props)) { $props = array_flip(Q::normalize($props)); } else { $props = $this->props2fields; } $error = array(); $mode = 'on_' . strtolower($mode); foreach ($this->validations as $prop => $policy) { if (!isset($props[$prop])) { continue; } if (!isset($data[$prop])) { $data[$prop] = null; } if (isset($this->belongsto_props[$prop]) && empty($policy['rules'])) { continue; } if (isset($policy[$mode])) { $policy = $policy[$mode]; } if (is_null($data[$prop])) { // 对于 null 数据,如果指定了自动填充,则跳过对该数据的验证 switch ($mode) { case 'update': if (isset($this->update_autofill[$prop])) { continue 2; } break; default: if (isset($this->create_autofill[$prop])) { continue 2; } break; } if (!$policy['policy']['allow_null']) { // allow_null 为 false 时,如果数据为 null,则视为验证失败 $error[$prop]['not_null'] = 'not null'; } elseif (empty($policy['rules'])) { continue; } } foreach ($policy['rules'] as $index => $rule) { $validation = array_shift($rule); $msg = array_pop($rule); array_unshift($rule, $data[$prop]); $ret = QValidator::validateByArgs($validation, $rule); if ($ret === QValidator::SKIP_OTHERS) { break; } elseif (!$ret) { $error[$prop][$index] = $msg; if (isset($policy['policy']) && !$policy['policy']['check_all_rules']) { break; } } } } return $error; }
/** * 验证元件值,如果验证失败,并且 $throw 参数为 true,则抛出异常 * * @param boolean $throw * * @return QForm_Element */ function validate($throw = true) { $this->_value_changed = false; $error = array(); if (!empty($this->_validations) && is_array($this->_validations)) { reset($this->_validations); $this->_value_is_valid = true; foreach ($this->_validations as $rule) { list($validation, $args, $msg) = $rule; array_unshift($args, $this->_value); if (!QValidator::validateByArgs($validation, $args)) { $this->_value_is_valid = false; $error[] = $msg; } } } else { $this->_value_is_valid = true; } if ($this->_value_is_valid) { $this->_validate_messages = null; } else { $this->_validate_messages = $error; if ($throw) { throw new QValidator_ValidateFailedException(array($this->id() => $error), array($this->id() => $this->_value)); } } }
/** * 导入数据后进行过滤,并返回验证结果 * * 通常调用 QForm 对象的 validate() 方法一次性导入整个表单的数据。 * * @param mixed $data 要导入并验证的数据 * @param array $failed 保存验证失败的信息 * * @return boolean 验证结果 */ function validate($data, &$failed = null) { if (!$this->_data_binding) { return false; } $this->_unfiltered_value = $data; $data = QFilter::filterBatch($data, $this->_filters); $this->_attrs['value'] = $data; if (!empty($this->_validations)) { $failed = null; $this->_is_valid = (bool) QValidator::validateBatch($data, $this->_validations, QValidator::CHECK_ALL, $failed); if (!$this->_is_valid) { $this->_error_msg = array(); foreach ($failed as $v) { $this->_error_msg[] = array_pop($v); } } $failed = $this->_error_msg; } else { $this->_error_msg = array(); $failed = null; $this->_is_valid = true; } return $this->_is_valid; }
/** * 是否是浮点数 * * @param mixed $value */ static function validate_is_float($value) { if (is_null(self::$_locale)) { self::$_locale = localeconv(); } $value = str_replace(self::$_locale['decimal_point'], '.', $value); $value = str_replace(self::$_locale['thousands_sep'], '', $value); if (strval(floatval($value)) != $value) { return false; } return true; }
/** * 导入数据后进行过滤,并返回验证结果 * * 通常调用 QForm 对象的 validate() 方法一次性导入整个表单的数据。 * * @param mixed $data * * @return boolean */ function validate($data) { $this->_unfiltered_value = $data; $data = QFilter::filterBatch($data, $this->_filters); $this->value = $data; if (!empty($this->_validations)) { $failed = null; $this->_is_valid = QValidator::validateBatch($data, $this->_validations, QValidator::CHECK_ALL, $failed); if (!$this->_is_valid) { $this->_error_msg = array(); foreach ($failed as $v) { $this->_error_msg[] = array_pop($v); } } } else { $this->_error_msg = array(); $this->_is_valid = true; } return $this->_is_valid; }